Jump to:

23014 Posts in 11873 Topics by 2830 members

General Questions

SilverStripe Forums » General Questions » Nooby Development Question.

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: 855 Views
  • JasonS
    Avatar
    Community Member
    14 Posts

    Nooby Development Question. Link to this post

    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.

    <?php
    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.

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Nooby Development Question. Link to this post

    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..

  • JasonS
    Avatar
    Community Member
    14 Posts

    Re: Nooby Development Question. Link to this post

    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.
    /layout/sidebar_testimonials
    /layout/sidebar_portfolio
    etc

    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.

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Nooby Development Question. Link to this post

    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?

  • JasonS
    Avatar
    Community Member
    14 Posts

    Re: Nooby Development Question. Link to this post

    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.

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Nooby Development Question. Link to this post

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

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Nooby Development Question. Link to this post

    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: Test.ss and AnotherTest.ss - 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/Page.ss template add:

    $SideElements

    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

  • JasonS
    Avatar
    Community Member
    14 Posts

    Re: Nooby Development Question. Link to this post

    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.

    Cheers

    Jason

    855 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.