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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

2.4x - Dynamic Include for Sidebars

Go to End

2 Posts   1166 Views


Community Member, 607 Posts

22 April 2013 at 8:16pm

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 template, and a Layouts/ template
-->MarketingPage.php - Extends page. Only has a Layouts/ template

I am using Twitter Bootstrap for the theme structure, so the common structure is found in the root 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 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

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


Community Member, 7 Posts

23 April 2013 at 10:08pm

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