3217 Posts in 853 Topics by 812 members
| Go to End | Next > | |
| Author | Topic: | 5850 Views |
-
A page head for each section

13 February 2009 at 5:21am
Hi!
I'm new to SilverStripe, but so far I like it very much. I now have a development problem which I don't know quite how to solve (unfortunately the SS documentation seems to be far from complete).
I have a site with six "sections" = six pages on Level 1 in the site structure. I want a distinct page head for each section, which would be inherited by the sub-pages of each respective section. For ease of maintenance I would like not having to create six different page types; I would rather have one page head which detects the section and replaces the shown contents accordingly.
I tried using $URLSegment as body id and showing only the correct head by means of css. This worked fine on the top level, but as each sub-page get their own url this solution was not practical. I have also tried experimenting with Level(1) etc. but found no workable solution.
Does anyone have a suggestion for a nice solution? I should also add that I am no programming genious -- I have dabbled in various simpler programming for a decade or two, but much of it is really over my head.
Yours,
MÃ¥ns -
Re: A page head for each section

13 February 2009 at 6:34am
my first thought was using different PageTypes, too - as you don't want to, things might be a little trickier.
a solution i can think of is to add an attribute to your page classlike sostatic $db = array(
"header" => "Enum('header1,header2,header3,header4,header5,header6,inherit','inherit')",
);then populate the options in a dropdown in the cms:
function getCMSFields() {
$fields = parent::getCMSFields();
$field = new DropdownField(
'header',
'select header:',
singleton('Page')->dbObject('header')->enumValues()
);
$fields->addFieldToTab('Root.Content.Main', $field);
return $fields;
}(see also http://doc.silverstripe.org/doku.php?id=dropdownfield)
now, in you Page_Controller class, define a function to retrieve the header value in templates:
function header() {
if($this->header == 'inherit') {
//some recursive function moving up the sitetree to detect the first page having header != 'inherit' and returning it
//can't figure out the exact code for now... you might also like to leave this on out and just set 'header' on every page...
} else {
return $this->header;
}
}now use $header in your template as a css class as you mentioned ...
hth
-
Re: A page head for each section

14 February 2009 at 12:25am Last edited: 14 February 2009 12:29am
There is a simpler way to do this. In your Page.php define whichever database fields you want for your header, in this example well just use a header image, but you can extend this as far as you need to.
mysite/code/Page.php
static $has_one = array (
'HeaderImage' => 'Image'
)function getCMS fields() {
if(!$this->Parent){
$fields->addFieldToTab("Root.Content.Main", new ImageField('HeaderImage'));
}}
then in your Page.ss template you can do this:<% control Level(1) %>
$HeaderImage
<% end_control %>The key to this is the if(!$this->Parent) which is checking that the page has no parents (i.e. is a root page) and if it is it adds the header field.
The only problem with this method is that pages which do not extend 'Page' wont have the header field, an example of this is the Redirector page. You can get around this by extending the Redirector page and adding in the above code, or adding it to the origonal redirector page or even easier just set a fallback image for pages which don't have the header field.
Anyway hope that helps
-
Re: A page head for each section

14 February 2009 at 11:44pm
you're completely right, aram, using <% Level(1) %> is the solution replacing my clumsy 'recursive function' approach.
just wanted to mention, the post py 'Mans' didn't clearly mention it's all about a simple image - in case it's more a piece of html, the mentioned way of using css classes (or alternatively, using an HTMLText field) may do the trick. -
Re: A page head for each section

16 February 2009 at 9:09pm Last edited: 16 February 2009 9:09pm
Thanks a lot for the help, guys! It's great to know that there is so much knowledge in these forums. Turns out I went with the multiple PageType solution after all -- there is indeed more than an image in each head, and it tended to get a little too complex (for me) using other solutions.
Again, thank you!
/MÃ¥ns
-
Re: A page head for each section

27 May 2009 at 10:57pm
hello,
@aram
I exactly tried it with your way (the different header image thing).
But it does not work.. when I upload my edited Page.php then I just get a white page.
And also dbflush doesnt work.What could be the problem?
--
The code
/**
* header images
*/
static $has_one = array (
'HeaderImage' => 'Image'
)
function getCMS fields() {
$fields = parent::getCMSFields();
if(!$this->Parent){
$fields->addFieldToTab("Root.Content.Main", new ImageField('HeaderImage'));
}
}The code is between
class Page_Controller extends ContentController {
...
} -
Re: A page head for each section

27 May 2009 at 11:17pm
Hi Martin
The getCMSFields needs to be in the Page model class (i.e. 'class Page extends SiteTree{}') not the controller, also there were a couple of typos in my post this should work:
static $has_one = array (
'HeaderImage' => 'Image'
)function getCMSFields() {
$fields = parent::getCMSFields();
if(!$this->Parent){
$fields->addFieldToTab("Root.Content.Main", new ImageField('HeaderImage'));
}return $fields;
} -
Re: A page head for each section

28 May 2009 at 2:40am
that works!
thank you very much
best regards, martin
| 5850 Views | ||
| Go to Top | Next > |




