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.

Themes /

Discuss SilverStripe Themes.

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

Holder for childpages to create layout


Go to End


14 Posts   16466 Views

Avatar
shrike

Community Member, 17 Posts

24 June 2011 at 12:08am

Edited: 24/06/2011 12:14am

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! :)

Avatar
martimiz

Forum Moderator, 1391 Posts

24 June 2011 at 12:57am

Edited: 24/06/2011 12:57am

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 Layout1.ss and Layout2.ss in your Templates/Includes/ directory. Then you could use the same includes to load the appropriate layout within your Pagetype1.ss and your Pagetype2.ss templates for the actual pages...

Avatar
martimiz

Forum Moderator, 1391 Posts

24 June 2011 at 2:26am

Edited: 24/06/2011 2:58am

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 %>
	$RenderAsChild
<% end_control %>

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

Avatar
shrike

Community Member, 17 Posts

24 June 2011 at 3:04am

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

Avatar
shrike

Community Member, 17 Posts

27 June 2011 at 11:36pm

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

Avatar
shrike

Community Member, 17 Posts

3 May 2012 at 8:30am

Edited: 03/05/2012 8:30am

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?

Go to Top