Jump to:

3393 Posts in 976 Topics by 852 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: 409 Views
  • Double-A-Ron
    Avatar
    Community Member
    601 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:

       <body>

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

           <div class="row-fluid">

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

                <h1>$Title.XML</h1>

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

             </div>

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

             </div>

           </div>

          </div>

       </body>
    </html>

    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 %>
                      $TemplateInclude
                   <% end_control %>
                <% end_if %>
             </div>

    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
    Avatar
    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:

    $this->renderWith($this->Template)

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

    409 Views
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.