Jump to:

5519 Posts in 1734 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Sidebar content/editable regions help (please)

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

Page: 1 2
Go to End
Author Topic: 3538 Views
  • AlaVive
    Avatar
    Community Member
    42 Posts

    Sidebar content/editable regions help (please) Link to this post

    I really have learned quite a bit from these forums, and from the great help at http://www.ssbits.com/.

    However, I am having a really difficult time getting static side content to appear in my pages.

    In the home page, the user needs to be able to modify the html for 3-4 varied content areas. One of these areas is the sidebar; the other 3 areas are locations where, if he has access to the html editor, he can use his limited html skills to enter other ads or whatever information he likes.

    The rest of the pages are to default to the sidebar navigation that seems to be working fine.

    The following is the code I've tried. Is there anyone out there who can help me with this?

    (I'm using v2.3.2...)
    ___________________

    StaticSidebar.php

    <?php

    class StaticSidebar extends HomePage {

    static $db = array(
    'SidebarHTML' => 'HTMLText'
    );

    static $has_one = array(
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();

    $fields->addFieldToTab("Root.Content.Useme", new HtmlEditorField( "SidebarHTML", "HTML Editor"));

    return $fields;
    }

    }

    class StaticSidebar_Controller extends HomePage_Controller{

    }
    ?>

    This successfully adds it to the CMS, giving me the option to create a new "Static Page."
    ___________________
    HomePage.php (I also tried this in the Page.php to no avail)

    <?php
    /**
    * Defines the GA HomePage page type
    */

    class HomePage extends Page {
       static $db = array(
       );
       
    static $has_one = array(
    );

    }

    class HomePage_Controller extends Page_Controller {
    public function GetStaticSidebar(){
          return DataObject::get_one("StaticSidebar");
    }
    ?>

    ___________________
    StaticSidebar.ss

    <% control GetStaticSidebar %>

    $SidebarHTML

    <% end_control %>

    ___________________
    In my HomePage.ss layout, I call it all this way:

    <div id="sidebar">
       <% include StaticSidebar %>
    </div>   

    I get no errors. The sidebar just fails to appear in the draft/published page. I'm thinking it's pretty obvious to someone who hasn't looked at this for days.

    I'm willing to start over from scratch if I can just get something that will work. I have to do this for another project in a few days, as well. And I'm a bit nervous that it's taking me this long in an effort to save the client time!

    Any help is GREATLY appreciated!

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Sidebar content/editable regions help (please) Link to this post

    I am not sure how you are doing it is the best way - do you really want a subclass of the homepage for the sidebar? If the sidebar content is for the homepage then you should just add a field to the homepage. Then calling that CMS Field is simple.

    Also Watch out for the use of Get in your template functions. Some times this can cause issues. I would change your GetStaticSidebar to StaticSidebarContent() for now just to be safe - also note that is different to your include name, if they are the same you could run into trouble as well.

  • AlaVive
    Avatar
    Community Member
    42 Posts

    Re: Sidebar content/editable regions help (please) Link to this post

    Thanks, willr. I did, indeed, over-complicate the matter in my state of hurried panic. The simple solution worked.

  • arthurdent
    Avatar
    Community Member
    153 Posts

    Re: Sidebar content/editable regions help (please) Link to this post

    I am also trying to add an editable sidebar to the right hand side of my homepage. I see that on this post Willr said:

    “If the sidebar content is for the homepage then you should just add a field to the homepage. Then calling that CMS Field is simple.”

    Can you please clarify? Do you mean simply add an include to the HomePage.ss file?

    Almost finished on this one - this is the final hurdle!!

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Sidebar content/editable regions help (please) Link to this post

    static $db = array(
    'SidebarHTML' => 'HTMLText'
    );

    To include the that field you just need to put $SidebarHTML in your template.

  • arthurdent
    Avatar
    Community Member
    153 Posts

    Re: Sidebar content/editable regions help (please) Link to this post

    Sorry Will, I’m afraid you have a SilverStripe newbie here (as if you hadn’t guessed)...

    So can you please clarify - where am I to put this code so I can access sidebar from CMS?

    static $db = array(
    'SidebarHTML' => 'HTMLText'
    );

    Thanks again!

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Sidebar content/editable regions help (please) Link to this post

    arthurdent - if you haven't already I recommend reading the tutorials. Tutorial 2 covers adding fields to the cms.

    So in this case do you want to add a sidebar area thats different on various pages or the same through out the site? The easiest way is to put that code 'SidebarHTML' => 'HTMLText' inside the $db array that is in the 'mysite/code/Page.php' file. Then like AlaVives code you need to add it to the CMS using the following code which also goes in that Page.php

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Sidebar", new HtmlEditorField( "SidebarHTML"));
    return $fields;
    }

    Then in your template (say for example the themes/blackcandy/templates/Page.ss file) you can call that new CMS field by adding $SidebarHTML

    The code for your page.php file should look something like this

    /mysite/code/Page.php

    <?php

    class Page extends SiteTree {

    static $db = array(
    'SidebarHTML' => 'HTMLText'
    );

    static $has_one = array(
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Sidebar", new HtmlEditorField( "SidebarHTML"));
    return $fields;

    }

    }

    class Page_Controller extends ContentController {

    }
    ?>

  • arthurdent
    Avatar
    Community Member
    153 Posts

    Re: Sidebar content/editable regions help (please) Link to this post

    Thanks heaps for this Will -- I'm wanting a custom sidebar on the HomePage file, not the entire site. I take it in this case the file I am working with is "mysite/code/HomePage.php"?

    Appreciate your comment about tutorials -- I have been through them, both on the SilverStripe forum and elsewhere, but I really had to hit the ground running on this project -- a month ago I'd never even heard of SilverStripe! While the tutorials are great, sometimes you just can't see the wood for the trees, as it were. Set out as you have presented it, I'm starting to get a clearer idea of how the different parts of the CMS and the templates interact. When I first looked at all the various files that came with a SS install it was a bit daunting -- while I've learned a lot about various file types and operating systems over the years and want to learn all that I can, initially I'm from a design background!

    That said, I'm really impressed with SS and the way it can be implemented into custom designs, so I'm really keen to get to grips with it!!

    3538 Views
Page: 1 2
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.