Jump to:

3401 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » Controls and If-blocks for specific tabs

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

Page: 1
Go to End
Author Topic: 2025 Views
  • Happysadhu
    Avatar
    Community Member
    33 Posts

    Controls and If-blocks for specific tabs Link to this post

    Hello,

    I've created a CMS Sidebar tab in Page.php with the following fields: SidebarTitle, SidebarContent, PhotoComment and Photo. I would like to create a specific "if block and control" function that would output the Sidebar.ss template only if there is any content added to any of the fields in the CMS.

    How would I create a function for a tab? I know how to do if the Sidebar is a pagetype (e.g., public function GetSidebar(){
    return DataObject::get_one("Sidebar");} but not for an array of fields in a custom tab,

    This is an example of the Sidebar.ss That I would like to create based on the Sidebar tab:

    <% if GetSidebar %>
       <% control GetSidebar %>
       <div id="Sidebar" class="typography">
       
                 <h3>$SidebarTitle</h3>
                 
                 <ul >
                   <li>$Photo.SetWidth(180)</li>
    <li>$PhotoComment</li>
                   <li>$SidebarContent</li>
                 </ul>
       </div>
       <% end_control %>
    <% end_if %>

    ********************************

    This is the relevant code from my Page.php:

    class Page extends SiteTree {
       
    Public static $db = array(
        'SidebarTitle' => 'Varchar(255)',
    'SidebarContent' => 'HTMLText',
    'PhotoComment' => 'Varchar(255)',
    );
       
       static $has_one = array(
        'Photo' => 'Image',
    );
       
    function getCMSFields() {
       $fields = parent::getCMSFields();
       $fields->addFieldToTab("Root.Content.Sidebar", new TextField("SidebarTitle", "Sidebar Item Title"));
       $fields->addFieldToTab("Root.Content.Sidebar", new ImageField('Photo'));
       $fields->addFieldToTab("Root.Content.Sidebar", new TextField("PhotoComment", "Photo comment"));
    $fields->addFieldToTab("Root.Content.Sidebar", new HtmlEditorField("SidebarContent", "Sidebar Content"));
    return $fields;
    }

    Thanks,

    Sam Miller

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Controls and If-blocks for specific tabs Link to this post

    A 'tab' is just a way to organize form fields within the CMS. They don't relate to any data structure within your website.

    You need to clarify whether a SideBar is supposed to be a separate object, or whether the sidebar just consists of some extra fields within the page object.

    If, for example, you want multiple pages to have the same SideBar (that is, have a one to many relationship), you need to create a new SideBar object. See this tutorial for information on handling relationships.

    If, on the other hand, you just want to include these fields as extra fields within the page, you can't 'control Sidebar' as such, since the fields are fields within the current page and not some other object. You could, however, create a 'checkSidebarFields' function that checks that the fields are filled out, and display the fields accordingly.

    eg:

    Within Page.php:

    // return true if any of the sidebar fields contain content, otherwise false
    function checkSidebarFields() {
       return ($this->SidebarTitle || $this->SidebarContent || $this->PhotoComment || $this->PhotoID)
    }

    Within the template:

    <% if checkSidebarFields%>
       <div id="Sidebar" class="typography">
          <h3>$SidebarTitle</h3>
          <ul >
             <li>$Photo.SetWidth(180)</li>
             <li>$PhotoComment</li>
             <li>$SidebarContent</li>
          </ul>
       </div>
    <% end_if %>

  • Happysadhu
    Avatar
    Community Member
    33 Posts

    Re: Controls and If-blocks for specific tabs Link to this post

    Hi Hamish,
    Thanks for your detailed response. I tried your suggested code:
    function checkSidebarFields() {
    return ($this->SidebarTitle || $this->SidebarContent || $this->PhotoComment || $this->PhotoID);
    }

    But the <% if checkSidebarFields %> in my template doesn't do anything? Is there something in the page.php function above that needs to be tweaked? I rebuilt my database, flushed my webpages, etc, but no luck.

    Much appreciated,
    Sam Miller

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