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.


Discuss SilverStripe Themes.

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

Holder for childpages to create layout


14 Posts   11357 Views


24 June 2011 at 12:08am (Last edited: 24 June 2011 12:14am), Community Member, 15 Posts

Hmm, but isn't this model then printing only one layout based on PageType and then I have to create the final layout again? I have idea to use already made layout. In this case product 1 page and product 2 page -this way I do not have to make layout again in page holder as the holder only should render the already made layout from the product page 1 and 2 (which are a child pages inside sitetree in CMS). If I have 10 child pages, the holder will be as tall as skyscraper. If I change the order in sitetree and then it automatically changes order in holder also.

Is holder/controller even the right word for whole idea? Dynamically include all child pages with layout after each other in one parent page? Humor: <% if hasChildren %> <% include all children pages with content and $Layout here inside parent in CMSSiteTree order %> <% end>

Has RenderWith() any use in here?

Thanx! :)


24 June 2011 at 12:57am (Last edited: 24 June 2011 12:57am), Forum Moderator, 1095 Posts

A very common approach in SilverStripe templating is the use of includes:

<% control Children %>
   <% if ClassName = PageType1 %>
      <% include Layout1 %>
   <% elseif ClassName = PageType2 %>
      <% include Layout2 %>
   <% end_if %>
<% end_control %>

You'd have and in your Templates/Includes/ directory. Then you could use the same includes to load the appropriate layout within your and your templates for the actual pages...


24 June 2011 at 2:26am (Last edited: 24 June 2011 2:58am), Forum Moderator, 1095 Posts

Getting back to this - it still is more complex then I thought... The examples would work - but only if in the original layout for the different pagetypes no Controller functions are used, because these are not available from within a control loop. You would need access to the different page controllers and have them render the templates.

So I've set up an idea that might work. It's very basic, and I've no idea how it would affect resources, but... :-)

In the Page class:

function RenderAsChild() {
   $class = $this->ClassName . "_Controller";
   $controller = new $class($this);
   return $controller->renderForHolderPage();

In the Page_Controller class

public static $LayoutTemplate = 'CustomPageLayout';

function renderForHolderPage() {
   $template = $this->stat('LayoutTemplate');
   if ($template) return $this->renderWith(array($template));
   else return '';

Now you need only set a different LayoutTemplate for each PageType (could even do it from _config) to determine what template to use - with full access to all controller methods. The HolderPage template just needs this:

<% control Children %>
<% end_control %>

[EDIT] forgot to return the template from the RenderAsChild method. Added that. Also need to use $this->stat to access appr. template...


24 June 2011 at 3:04am Community Member, 15 Posts

Now we are getting there! Great idea, got to check this out.


27 June 2011 at 11:36pm Community Member, 15 Posts

This works perfectly! Thanx for help. Only Pages with WidgetArea are not working, but it is not a problem in this case.


3 May 2012 at 8:30am (Last edited: 3 May 2012 8:30am), Community Member, 15 Posts

Ok, new ideas needed again to solve problem with <% include SomePart %>. If for example page type "1Col" is used without this renderforHolderpage-method, includes works fine. But when the "1Col" is used as a child page under this holder-page, includes are not working anymore.

Is it possible to make some kind of add on to this code to make it work, or is it just a limitation of the system?