21489 Posts in 5783 Topics by 2621 members
| Go to End | ||
| Author | Topic: | 3287 Views |
-
Re: Includes from php instead of template

24 November 2009 at 1:38pm
What if you can't get an <% else_if %> to work?
Trying a real basic one at the moment that just won't budge..
<% if InSection(home) %>
<% include SideBarWidget %>
<% else_if InSection(sitemap) %>
<% include SideBarWidget %>
<% else_if InSection(privacy-policy) %>
<% include SideBarWidget %>
<% else_if InSection(disclaimer) %>
<% include SideBarWidget %>
<% else_if InSection(register-interest) %>
<% include SideBarWidget %>
<% else_if InSection(refer-us) %>
<% include SideBarWidget %>
<% else %>
<% include Menu2 %>
<% end_if %> -
Re: Includes from php instead of template

26 November 2009 at 5:26am Last edited: 26 November 2009 5:27am
Whoa whoa whoa, everyone. This is getting WAY overcomplicated. Use a custom control instead.
YourPage.php (Controller section)
public function WhatShouldBeIncluded() {
$return = array();
// List out all the things want a sidebar for
$giveMeASidebar = array('home', ....... 'privacy-policy','disclaimer','register-interest','refer-us');
// Get a list of all parent page URLSegments (which is what InSection() does, see SiteTree.php)
$allParentSegments = array();
$page = $this;
while($page) {
$allParentSegments[] = $page->URLSegment;
$page = $page->Parent;
}
if( !empty( array_intersect($giveMeASidebar,$allParentSegments) ) ) {
$return['UseSidebar'] = true;
}
return $return;
}YouPage.ss
<% control WhatShouldBeIncluded %>
<% if UseSidebar %>
<% include SideBarWidget %>
<% else %>
<% include Menu2 %>
<% end_if %>
<% end_control %>You could even shorten that loop to <% if WhatShouldBeIncluded.UseSidebar %>. Using a control gives you full PHP control on the backend and minimized hacky template coding.
Now on to the turkey!
| 3287 Views | ||
| Go to Top |

