Jump to:

23472 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Making Footer Dynamic

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

Page: 1
Go to End
Author Topic: 2554 Views
  • sca123
    Avatar
    Community Member
    61 Posts

    Making Footer Dynamic Link to this post

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Making Footer Dynamic Link to this post

    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.

  • pinkp
    Avatar
    Community Member
    168 Posts

    Re: Making Footer Dynamic Link to this post

    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.

  • martimiz
    Avatar
    Forum Moderator
    1082 Posts

    Re: Making Footer Dynamic Link to this post

    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

  • _AJ
    Avatar
    Community Member
    2 Posts

    Re: Making Footer Dynamic Link to this post

    Worked perfectly - cheers martimiz

    2554 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.