Jump to:

5537 Posts in 1737 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Changing code based on Parent type

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

Page: 1
Go to End
Author Topic: 1082 Views
  • bones
    Avatar
    Community Member
    104 Posts

    Changing code based on Parent type Link to this post

    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

  • Nathan Cox
    Avatar
    Community Member
    99 Posts

    Re: Changing code based on Parent type Link to this post

    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

    1082 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.