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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Set mainTemplate at runtime?


Go to End


3 Posts   2457 Views

Avatar
fabilo

Community Member, 10 Posts

10 March 2010 at 10:30pm

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

Community Member, 63 Posts

14 June 2010 at 3:04am

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

Community Member, 63 Posts

14 June 2010 at 3:35am

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