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.

Customising the CMS /

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

Changing code based on Parent type


Go to End


2 Posts   1710 Views

Avatar
bones

Community Member, 110 Posts

23 July 2009 at 11:57pm

My website has two distinct sections ("cars" and "bikes"), so I have made CarsHolder and BikesHolder page types with corresponding children. Each section has a different image. However, for pages which can't be CarsArticle or BikesArticle (ie User Defined Forms), I could do with some code to go onto Page.ss which will automatically select the correct image based upon the Parent page type.

ie: A User Defined Form with "CarsHolder" as it's parent will load an image of the car for its header.

I had hoped that something along the lines of the code below would work, but I don't know the correct code:

<% if $Parent = CarsHolder %>
<div id="carsheaderpic">Cars</div>
<% else %>
<div id="bikesheaderpic">Bikes</div>
<% end_if %>

Any pointers greatly appreciated :)

Avatar
Nathan Cox

Community Member, 99 Posts

31 July 2009 at 4:39pm

Maybe try something like this:

<% control Level(1) %>
	<div id='Header' class='$ClassName'>$Title</div>
<% end_control %>

the Level(1) control will get the root ancestor, so it'll work in sub sub pages where Parent won't. Just change the CSS to use BikesHolder and CarsHolder instead of bikesheaderpic carsheaderpic.

Just noticed this post is a bit late, but hopefully at least someone will find it helpful