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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

2.4x - Dynamic Include for Sidebars


Go to End


2 Posts   1806 Views

Avatar
Double-A-Ron

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

Avatar
amalet

Community Member, 7 Posts

23 April 2013 at 10:08pm

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