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.

General Questions /

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

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

Making Footer Dynamic


Go to End


5 Posts   3572 Views

Avatar
sca123

Community Member, 61 Posts

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.

Avatar
bummzack

Community Member, 904 Posts

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.

Avatar
pinkp

Community Member, 182 Posts

28 May 2011 at 2:07am

Edited: 28/05/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.

Avatar
martimiz

Forum Moderator, 1391 Posts

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

Avatar
_AJ

Community Member, 2 Posts

17 July 2012 at 10:37pm

Worked perfectly - cheers martimiz