24
Mar
14
Create a Static, CMS editable sidebar
What it Does
It's a static sidebar that will hold a couple of links, an image and some HTML text. Your's can hold anything you like and is easily adaptable to a 'Common Links', 'favourite Images' or 'User Notice' blocks.
Requirements
2.2 +
Attached Files
Complete.zip(3 KB)
Preperation
We need to create 2 new files for this tutorial, one for the php (Model & Controller) called StaticSidebar.php and another for the template (View) called StaticSidebar.ss.
You can leave StaticSidebar.ss blank for now, but inside StaticSidebar.php add the following:
mysite/StaticSidebar.php
Part 1: Setting up the model
Our static sidebar is going to have an external link (TextField), and internal link (SitetreeDropdownField) and an Image as well as using the pages default Content (HTMLEditorField) for its HTML text. So lets add our database model to the class.
Now lets add the CMS fields so that we can edit the sidebar:
Note. Even though our has_one relationship was called “InternalLink" we call the TreeDropdown “InternalLinkID” as this is the column in the StaticSidebar table that it needs to save it's contents into. This is a slight inconsistency with Silverstripe, as we don't need to call our ImageField 'ImageID' even though it is the same principle.
The last thing to do is to add a line which stops the user creating children under our sidebar and also setting the default for 'ShowInMenus' to be false so that our sidebar page doesnt show up as an actual page on our site.
Now run yoursite.com/dev/build (in 2.2X run yoursite.com/db/build?flush=1). You will see the new fields being added and you can now log into the
Part II: Creating the Template
We are going to intergrate our sidebar into the default blackcandy theme, but it should be an almost indentical process to add it to your theme, just apply your own structure to it. You could also just as easily make it part of the footer, header or any other part of the page you fancy as we havnt actually defined anything that says where the information stored in the model needs to be on the page. Silverstripe gives up total freedom.
For our tempalte we can just copy and adapt the existing Sidebar template found in themes/blackcandy/templates/Includes/Sidebar.ss. minus the outermost div (id="Sidebar"). So copy and paste that into our StaticSidebar.ss file we created earlier and make it look like this:
themes/blackcandy/templates/Includes/StaticSidebar.ss
So first up you will notice that we are calling a function 'GetStaticSidebar'. We havnt created this function yet, we do that in the next part, but all it does is return our StaticSidebar page so that we can use the data held in. So we first check if our StaticSidebar exists and if it does we enter a control context for it, allowing us to use all the variables first hand (i.e. $Image instead of $GetStaticSidebar.Image).
Then we remove the outermost div so that we dont't get 2 #sidebars and then just use the unordered list (<ul>) and enter each item as a list item (<li>). We use $InternalLink.Link because $InternalLink on it's own returns the actual page that we are linking to where as we want the link to that page in the same way as we use $Link in our menus. We use the same method to get the pages title, $InternalLink.MenuTitle. We also set the image to be 80px wide by calling $Image.SetWidth(80).
part III: Adding it to our Page
Finally we just need add the sidebar to our page template and create the function that will return our StaticSidebar for use in all our pages. Because we want to use it accross the whole site, it makes sense to put this function in the Page_Controller so that all our pages that extend Page will have access to this function. In the case of the blackcandy theme we need to add our sidebar underneath the existing menu(2) sidebar. To do this we will include our template in the Sidebar.ss file which is in turn included in our Layout/Page.ss template. We then need to make some slight alterations to the <% if %> block which surrounds the Sidebar include as we now want to draw the sidebar even if there is no level 2 menu to display.
So first lets add the function to our Page.php file:
mysite/Page.php
All this function does is grabs the first StaticSidebar it can find and returns it. This is all we need as there will only be one of these sidebars in the site. If you need to have more then one, you will need to create a function which can filter the result based on a parameter that you define. So for example calling <% GetStaticSidebar(right) %> might use a function that has the database call DataObject::get("StaticSidebar", "Type = $Side"), where $Side is the variable passed into the function (in this case "right").
Finally we need to include our StaticSidebar.ss tempalte in the Sidebar.ss template and then make some slight alterations to the Layout/Page.ss template to ensure the Sidebar.ss is always included. So open up Includes/Sidebar.ss and make the following changes:
themes/blackcandy/templates/Includes/Sidebar.ss
Now we just need to remove the if statment from our Layout/Page.ss:
themes/blackcandy/templates/Layout/Page.ss
Remember you will need to add the <% include Sidebar %> to all your new Layout files if you want any sidebar on those pages.
About the Author
Name: Aram Balakjian
Website: http://www.aabweb.co.uk
Aram is a web designer/developer running London based agency aab web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.
Comments (10)
-
This works great as a stand alone page - I\\\'ve been trying to get this to work as part of staticsidebar. Any ideas how to get this to work?
Posted by Spiggley, 30/08/2010 4:13am (5 days ago)
-
free
credit report online
http://trig.com/credit/biographyPosted by free credit report online http://trig.com/credit/biography, 29/08/2010 10:17am (5 days ago)
-
I'd like to replace the linked text with images. I've searched the forum for how to do this and I've tried out what I've seen but i keep getting error messages.
Posted by pll fm transmitter, 12/08/2010 10:02pm (22 days ago)
-
Traced it back to a reference to Page.php on line 20 (which is now changed to HomePage -- now it's falling over at the first reference to HomePage on line 6 og HomePage.php -- any pointers now please? As you may have guessed, I'm a newbie to SilverStripe... any assistance appreciated!
Posted by Mike, 17/11/2009 3:10am (10 months ago)
-
Hi Aram. Great tutorial - just struck one problem... I want the sidebar to appear on the site homepage. I added content from your mysite/Page.php to my mysite/HomePage.php, and got the following fatal error:
Fatal error: Cannot redeclare class Page in /Users/mikehenden/Sites/SilverStripe/mysite
/code/HomePage.php on line 41
Line 42 is a closing argument bracket - }
Can you give me any pointers?Posted by Mike, 17/11/2009 1:22am (10 months ago)
-
There is a simpler solution for basic use:
1. Create a page (whatever type you want) named SidebarPage.
2. In the Behavior tab, uncheck the "Show in menus?" check.
3. In your template, put <div id="Sidebar" class="typography">$Page(SidebarPage).Content</div>
4. Edit SidebarPage as desired.
Hope this is useful.
--RickPosted by RickT, 06/10/2009 3:52pm (11 months ago)
-
I did everything you explained but the sidebar does not show up in the CSM as shown in tutorial. it shows up in my create page drop down so i can make a page for it. i even used your completed files and it didn\'t work. what could i be doing incorrect?
Thanks in advancePosted by Brennan, 18/07/2009 9:20am (1 year ago)
-
I did everything you explained but the sidebar does not show up in the CSM as shown in tutorial. it shows up in my create page drop down so i can make a page for it. i even used your completed files and it didn\'t work. what could i be doing incorrect?
Thanks in advancePosted by Brennan, 18/07/2009 9:03am (1 year ago)
-
Hi, I'm new to SS and I love this CMS. The only issue I' m having is customizing the sidebar,
I'd like to replace the linked text with images. I've searched the forum for how to do this and I've tried out what I've seen but i keep getting error messages. Can anybody help me with this?
Best Regards,
CaribbeanGirlPosted by Caribbean Girl, 02/06/2009 4:28pm (1 year ago)
-
I have followed the instructions and when I try to build the database I get error on line 21 for some reason.
I even tried to upload the file from zip file and it still gives me error.
Govindji Patel
Posted by Govindji Patel, 16/05/2009 12:50pm (1 year ago)
RSS feed for comments on this page RSS feed for all comments