21295 Posts in 5734 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 2236 Views |
-
Making Footer Dynamic

16 May 2009 at 9:03pm
I'm pretty new to Silverstripe and just setting up my first Silverstripe site..
I have setup a page called "Footer" in the CMS and have placed some relevant text in the "Content" field.
I would now like to include this content in my template - Similar to a global content block.
Any help would be appreciated.
-
Re: Making Footer Dynamic

16 May 2009 at 10:02pm
Hi
I would do that slightly different. Instead of creating a Page called "Footer", create a new class named HomePage which you then assign to the Home-Page (as described in this tutorial: http://doc.silverstripe.com/doku.php?id=tutorial:1-building-a-basic-site#using_a_different_template_for_the_home_page).
The HomePage class can then be used to store "global" fields like the footer, since there's mostly just one single HomePage. So your HomePage class could look like this:
?php
class HomePage extends Page
{
public static $db = array(
'Footer' => 'HTMLText'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab(
'Root.Content.Main',
new HtmlEditorField('Footer')
);
return $fields;
}
}class HomePage_Controller extends MainPage_Controller {
}That will take care of the Footer content. You can edit it, by editing the HomePage...
Now you just need a method to access your Footer-Content on any page. Here's how (make sure this in your Page.php file):class Page {
... contents of Page ...
}class Page_Controller extends ContentController
{
// this is the important part
public function Footer(){
$obj = SiteTree::get_one('HomePage');
return $obj->Footer;
}
}Now you can simply place $Footer anywhere in your template and it should output the footer content.
-
Re: Making Footer Dynamic

28 May 2011 at 2:07am Last edited: 28 May 2011 2:07am
I did this with 'HTMLText' but can not do it with 'Image' can you point out if there is something wrong in my code? thanks!
SliderPage.php
?php
class HomePage extends Page
{
public static $has_one = array(
'Slider' => 'Image'
);public function getCMSFields()
{
$fields = parent::getCMSFields();$fields->addFieldToTab(
'Root.Content.Main',
new ImageField('Slider')
);return $fields;
}
}class HomePage_Controller extends MainPage_Controller {
}Page.php
class Page {
... contents of Page ...
}class Page_Controller extends ContentController
{
// this is the important part
public function Slider(){
$obj = SiteTree::get_one('HomePage');
return $obj->Slider;
}
}Adding $Slider to the template has no effect.
-
Re: Making Footer Dynamic

28 May 2011 at 9:33pm
For things like this, the SiteConfig is probably the best place. It's quite easy to decorate and every page template can access it
http://doc.silverstripe.org/sapphire/en/reference/siteconfig
| 2236 Views | ||
|
Page:
1
|
Go to Top |



