Jump to:

3447 Posts in 1032 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » 2.4x - Dynamic Include for Sidebars

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

Page: 1
Go to End
Author Topic: 486 Views
  • Double-A-Ron
    Community Member
    604 Posts

    2.4x - Dynamic Include for Sidebars Link to this post

    I've searched and searched, but I haven't found much help. I think it may be more of about how I'm approaching this whole template structure:

    Page.php - has a Page.ss template, and a Layouts/Page.ss template
    -->MarketingPage.php - Extends page. Only has a Layouts/MarketingPage.ss template

    I am using Twitter Bootstrap for the theme structure, so the common structure is found in the root Page.ss file. Simplified version here:


          <!-- Page Container -->
          <div class="container" id="main-content-container-trans">

           <div class="row-fluid">

             <!-- Main Content Container -->
             <div class="span9">


                <div class="row-fluid main-text-container">


             <!-- Sidebar -->
             <div class="span3">





    The challenge is the contents of the Sidebar div. While the heirachy of this structure works just fine for $Layout should I make a new page type, the sidebar content will be different depending on the page type. So what I need is dynamic include mechanism that is fed from the page type class (in this instance, MarketingPage.php).

    I have found several ways to store and get the names of the include files to the template, using a DataObject that allows you to select a template file from the CMS (the dataobject reads the Includes directory) using ComplexTableField. Then, in this DataObject I have the following method:

    public function TemplateInclude() {
          return $this->renderWith($this->Template);

    And in the Page.ss template above, I do this:

    <!-- Sidebar -->
             <div class="span3">
                <% if SideBarBoxes %>
                   <% control SidebarBoxes %>
                   <% end_control %>
                <% end_if %>

    Now, this works. It includes any templates selected in the CMS. However the problem is that include template has no knowledge of the page that is including it, and won't display relevant data such as Children for navigation (as an example).

    To make it clear, imagine the selected include was just a basic nav called Navigation.ss:

    <% control Menu(2) %>
              <% if Children %>
                  <li class="$LinkingMode"><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode levela"><span><em>$MenuTitle</em></span></a>
               <% else %>
                    <li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode levela"><span><em>$MenuTitle</em></span></a>
                <% end_if %>   

    This will appear blank using my dynamic method above. But if I had code it onto the template using <% include Navigation %>, it works just as expected, displaying links to the current page's children.

    Any ideas?

  • amalet
    Community Member
    7 Posts

    Re: 2.4x - Dynamic Include for Sidebars Link to this post

    In your TemplateInclude function on your SidebarBoxes DataObjects you're calling:


    That's rendering the template with the DataObject as the main scope. The getMenu() function is part of ContentController. That's why it works when you include the template directly from your main page template: because the Page's Controller, and therefore getMenu(), is accessible in the template scope. Try:

    public function TemplateInclude() {
    $controller = Controller::curr();
    return $controller->renderWith($this->Template);

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