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.

Template Questions /

Controls and If-blocks for specific tabs


Reply


3 Posts   2154 Views

Avatar
Happysadhu

Community Member, 33 Posts

7 August 2009 at 8:26am

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

Avatar
Hamish

Community Member, 712 Posts

7 August 2009 at 11:13am

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 [url=http://doc.silverstripe.org/doku.php?id=tutorial:5-dataobject-relationship-management]this tutorial[/url] 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 %>

Avatar
Happysadhu

Community Member, 33 Posts

8 August 2009 at 5:49am

Edited: 08/08/2009 5:58am

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