Jump to:

23374 Posts in 18175 Topics by 2866 members

General Questions

SilverStripe Forums » General Questions » Includes from php instead of template

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

Page: 1 2
Go to End
Author Topic: 3770 Views
  • rokryan
    Avatar
    Community Member
    13 Posts

    Re: Includes from php instead of template Link to this post

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

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Includes from php instead of template Link to this post

    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!

    3770 Views
Page: 1 2
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.