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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Includes from php instead of template


Reply

10 Posts   3854 Views

Avatar
rokryan

24 November 2009 at 1:38pm Community Member, 13 Posts

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 %>

Avatar
dalesaurus

26 November 2009 at 5:26am (Last edited: 26 November 2009 5:27am), Community Member, 283 Posts

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!