Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

Set mainTemplate at runtime?


Reply

3 Posts   1521 Views

Avatar
fabilo

10 March 2010 at 10:30pm Community Member, 10 Posts

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

Avatar
Wilson

14 June 2010 at 3:04am Community Member, 63 Posts

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

Avatar
Wilson

14 June 2010 at 3:35am Community Member, 63 Posts

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;
}