Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Set mainTemplate at runtime?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1514 Views
  • fabilo
    Avatar
    Community Member
    10 Posts

    Set mainTemplate at runtime? Link to this post

    Hey guys, is it possible to set the mainTemplate that is used in a page controller function?

    I tried using 'return $this->renderWidth("Ajax");' but to no avail. It just displays the Ajax.ss template as the $Layout for the standard mainTemplate (Page.ss).

    I checked the core SSViwer.php code and saw a guard in there - so tried using '$this->renderWidth("Ajax.ss")' but it returns an error:
    [Warning] filemtime() [function.filemtime]: stat failed for Ajax.ss

    Can anyone help me? I'm truely stumped.

    Cheers

  • Wilson
    Avatar
    Community Member
    63 Posts

    Re: Set mainTemplate at runtime? Link to this post

    I have the same issue. I would have assumed this would be common, but I'm not finding the answer. Basically, I need to be able to load the full page if called by URL but if called via Ajax then not load the navigation, header, etc but only body content.

    It seems you would render the page with a Layout file like Ajax.ss rather than Page.ss, in which Ajax.ss is simply:

    $Layout

    Then in Page.php, you would have:

          if(Director::is_ajax()) {
             $this->isAjax = true;
             $this->renderWith("Ajax");
          } else {
             $this->isAjax = false;
          }

    Of course, renderWith('Ajax') only pertains to the template, but not overall Layout. How do you switch Layouts?

    Thanks!
    Wilson

  • Wilson
    Avatar
    Community Member
    63 Posts

    Re: Set mainTemplate at runtime? Link to this post

    Well, I found a workaround. In templates/Page.ss you can add this condition:

    <% if isAjax %>
       $Layout
    <% else %>
    normal page...

    As long as you have this in Page.php

    if(Director::is_ajax()) {
    $this->isAjax = true;
    } else {
    $this->isAjax = false;
    }

    1514 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.