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.

Archive /

Our old forums are still available as a read-only archive.

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

ParentID if lowerlevel pages, ID if not? Help!


Go to End


7 Posts   4105 Views

Avatar
dancrew32

Community Member, 15 Posts

25 April 2008 at 11:26am

trying to make a different header image for specific pages based on their parent id..

the problem is that When you're on the ParentID page, it outputs '0' for a value.

How would I make it write it's own ID (when on the top level page) and write it's ParentID when you're on a child page?

id="header" class="ban$ParentID"

Thanks in advance!

Avatar
Blackdog

Community Member, 156 Posts

25 April 2008 at 1:32pm

That is going to get broken pretty quick if you have to keep making classes for each page.

Have you thought about using a enum select for pages to select from a defined list of classes.. you could even use a dataobject list to setup new classes to make sure it is kept current.

Avatar
Sam

Administrator, 690 Posts

25 April 2008 at 10:13pm

dancrew: I recommend that you make a little function on your Page class, HeaderClass:

it could be this:

function HeaderClass() {
if($this->ParentID) return "banner-" . $this->ParentID;
else return "banner-" . $this->ID;
}

But Blackdog's suggestion is better. For that, you could make an enum field called HeaderClass instead of the HeaderClass() function. :-)

Avatar
dancrew32

Community Member, 15 Posts

26 April 2008 at 7:14am

Sam and Blackdog, I really appreciate the help.

Sam, I am new to this MVC deal, and I'm curious where you might implement the script you provided below. Would it be in mysite/code/page.php? How would I call it on my theme/page.ss?
$HeaderClass?

Sorry, new to this part of SS still. Thanks for any help you can provide. =]

Avatar
Sam

Administrator, 690 Posts

26 April 2008 at 11:45am

Yes on both counts. HeaderClass() should either be inside class Page or class Page_Controller.

If it's inside class Page, then you will be able to access it inside a block such as <% control Menu(1) %> - getting the HeaderClass for each of the menu items. However, I'm sure not that you would ever want to do this.

If you put it inside class Page_Controller, then you will only be able to access it at the top level. For methods that don't make any sense to be used inside a <% control %> block, we usually put them on the controller.

Avatar
dancrew32

Community Member, 15 Posts

6 May 2008 at 3:57am

Edited: 06/05/2008 3:58am

Sorry, hopefully this is the last question... I just don't quite get how to implement the control from Page_Controller. Would it be like this?

<div id="header" class="<% control HeaderClass() %><% end_control %>">

my page controller class:

class Page_Controller extends ContentController {
function init() {
parent::init();

Requirements::themedCSS("layout");
Requirements::themedCSS("typography");
Requirements::themedCSS("form");
}
function HeaderClass() {
if($this->ParentID) return "banner-" . $this->ParentID;
else return "banner-" . $this->ID;
}
}

(btw, this doesn't output anything in the class="" section for some reason.. any ideas?)

Avatar
Sean

Forum Moderator, 922 Posts

6 May 2008 at 9:06am

Edited: 06/05/2008 9:08am

You're heading in the right direction.

Take the parenthesis off the <% control HeaderClass() %> inside the template, that isn't required and may be breaking. Also, you should be just returning $HeaderClass, as it's a single piece of text. The <% control HeaderClass %> block is used for a set of data returned, as opposed to a single string.

e.g.

<div id="header" class="$HeaderClass">