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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Making Footer Dynamic


Reply

5 Posts   2568 Views

Avatar
sca123

16 May 2009 at 9:03pm Community Member, 61 Posts

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.

Avatar
banal

16 May 2009 at 10:02pm Community Member, 901 Posts

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.

Avatar
pinkp

28 May 2011 at 2:07am (Last edited: 28 May 2011 2:07am), Community Member, 168 Posts

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.

Avatar
martimiz

28 May 2011 at 9:33pm Forum Moderator, 1091 Posts

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

Avatar
_AJ

17 July 2012 at 10:37pm Community Member, 2 Posts

Worked perfectly - cheers martimiz