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

Nooby Development Question.


8 Posts   930 Views


26 June 2009 at 6:45pm Community Member, 14 Posts

Hey, I was hoping that someone could help with this. I have created a new form field in my db called Sidebar. In sidebar I can put in keywords. E.g. news blogroll etc

The idea is that when the template is loaded, I will read what has been entered and output these various sidebar elements.

I have two questions.

I am aware I need to make a function called SideBar. Then I can use $SideBar in the template. Where does this function go? Secondly how do I gather and read this sidebar field that was inputted?

This is what I have so far.

class TwoColumnPage extends Page {
   static $db = array(
      'Sidebar' => 'Text'
   static $has_one = array(
   function getCMSFields() {
      $fields = parent::getCMSFields();
      $fields->addFieldToTab('Root.Content.Main', new TextField('Sidebar'), 'Content');
      return $fields;

class TwoColumnPage_Controller extends Page_Controller {


The second question is the best way to output these sidebar elements. I was going to put each Sidebar Element into the LayOut folder in my template.

e.g. sidebar_news, sidebar_blogroll

If I did this, is there a way to call these from a function in the file above? Do I need to return $SideBar_News, $SideBar_Blogroll or is this not possible.

Any advice given on both of these points is very much greatly appreciated.


26 June 2009 at 7:07pm Community Member, 395 Posts

Hi Jason,

First and foremost I will suggest you to change that name to something else, because the blog itself has a sidebar variable, so this can lead to conflicts if you install a blog in the future.

And to retrieve the Sidebar content you dont need to create a function. You can just retrieve it with putting $Sidebar on your template files.

"The second question is the best way to output these sidebar elements. I was going to put each Sidebar Element into the LayOut folder in my template. "

Now I dont think you need something like this, cause you can make the Sidebar appear on your pages simply adding the $Sidebar variable on your template file.

If you need to change the layout from one page to another you can define many page types and then add custom layouts for that.

if you need any other help welcome..


26 June 2009 at 7:18pm (Last edited: 26 June 2009 7:19pm), Community Member, 14 Posts

Hello Nivanka, thanks for the speedy reply. I can change the name of the sidebar shortly.

The thing is that from page to page the sidebar will change. I do not want to create 10 different page types with 10 slightly different sidebars. What I want to be able to do is create an input box (and later a bunch of checkboxes when I work out how to do it :)) that allow's me to define what goes into the sidebar.

Currently, I have the following that needs to go into the sidebar across various pages. Testimonials, Portfolio, Contact, Links. I will need more in the future.

I am going to create templates for each of these.

What I would like to do is be able to specify 'Portfolio Testimonials' in the sidebar input box.

I then need to create a function that will only show Portfolio and Testimonials in the 2nd column of my website. I hope this makes sense. I am just looking for a way to process the input box and display the relevant templates based on what the page requires.


27 June 2009 at 3:07am Forum Moderator, 1095 Posts

To get an idea - you have this:

- a couple of templates for different sidebar elements
- a textfield containing a comma-separated list of element-names

Would the templates contain dynamic content and/or actually do something? In that case would your page handle the code (not easily expandable), or would you create separate classes for each element?


27 June 2009 at 4:00am Community Member, 14 Posts

Hello martimiz, I think in time the goal would be for them all to have their own functionality. For now though, I am just getting used to SilverStripe and I just want to it to work. Any advanced features can come in over time.


27 June 2009 at 5:18am Forum Moderator, 1095 Posts

As I'm thinking about something similar, I'll have a brainstorm about the first most simple steps, I'll be back...


27 June 2009 at 6:24am (Last edited: 27 June 2009 6:25am), Forum Moderator, 1095 Posts

OK, I'm back :-) This is what I've come up with so far. Mind - it's really simple, and should probably be forgotten right away, but it's a nice learner at least :-)

First: create two templates in templates/Includes: and - both containing some plain text or whatever.

Now create a textfield in the page class (as you did in fact):

   public static $db = array(
      'SideElementList' => 'Varchar'

   public function getCMSFields() {
      $fields = parent::getCMSFields();
      $fields->addFieldToTab("Root.Content.SideElements", new TextField('SideElementList', 'Enter a list of side elements'));
      return $fields;

Then create the following function in the PageController class, that will return your templates:

   function SideElements() {
      $arraySideElements = explode(',', $this->SideElementList);
      $result = '';
      foreach ($arraySideElements as $element) {
       $result .= $this->renderWith($element);
      return $result;

Somewhere in your templates/ template add:


and then do a /dev/build/?flush=1

You now have a super simple setup for two elements: Test and AnotherTest. You can fill out your textfield in the CMS like so: Test,AnotherTest. The appropriate templates will be loaded. Mind upper and lowercase: must be equal to the template names!

Of course you need to do all kinds of checking and trimming in the SideElements function, but hey :-)


27 June 2009 at 9:15am Community Member, 14 Posts

Thx a lot. That is perfect. This is what I wanted to create but I lacked the knowledge to do it. I will fill in the gaps and further develop.