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.

Archive

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

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

ParentID if lowerlevel pages, ID if not? Help!


Reply

7 Posts   2988 Views

Avatar
dancrew32

25 April 2008 at 11:26am Community Member, 15 Posts

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

25 April 2008 at 1:32pm Community Member, 156 Posts

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

25 April 2008 at 10:13pm Administrator, 685 Posts

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

26 April 2008 at 7:14am Community Member, 15 Posts

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

26 April 2008 at 11:45am Administrator, 685 Posts

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

6 May 2008 at 3:57am (Last edited: 6 May 2008 3:58am), Community Member, 15 Posts

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

6 May 2008 at 9:06am (Last edited: 6 May 2008 9:08am), Forum Moderator, 921 Posts

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">