Jump to:

3392 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Conditional Sidebar <% if Menu(2) || Photo || SidebarContent %> doesn't work

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

Page: 1 2
Go to End
Author Topic: 1937 Views
  • Samba Sam
    Avatar
    Community Member
    85 Posts

    Conditional Sidebar <% if Menu(2) || Photo || SidebarContent %> doesn't work Link to this post

    Hi,
    I am trying to get a sidebar to appear based on one of the following conditions:
    There are child pages (i.e., Menu2 items), or a photo or content entered into the CMS sidebar tab, which I've added to the Page.php file.

    I can get <% if Menu(2) %> and <% if Photo || SidebarContent %> to work on their own. But I get a parsing error if I combine them.
    This is the code in my page.ss template. Any ideas?

    <% if Menu(2) || Photo || SidebarContent %>
          <div id="ContentNarrow">   
             $Layout
          </div>
          <div id="SidebarR">
          <% if Menu(2) %>
             <% include Menu2 %>
          <% end_if %>
          <% if Photo || SidebarContent %>
             <% include SidebarStuff %>
          <% end_if %>   
          </div>
          
       <% else %>
             $Layout
       <% end_if %>

    Thanks,
    Sam

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Conditional Sidebar <% if Menu(2) || Photo || SidebarContent %> doesn't work Link to this post

    I think you can only do 1 comparison. Eg if Menu(2) || Photo. I dont think the template parser supports more then that

    You would have to write your own PHP function for it <% if ShowSidebar %>

    .. mysite/code/Page.php

    function ShowSidebar() {
    return (.... your or statement) ? true : false;
    }

  • Samba Sam
    Avatar
    Community Member
    85 Posts

    Re: Conditional Sidebar <% if Menu(2) || Photo || SidebarContent %> doesn't work Link to this post

    What would the "your or statement" in the brackets look like?

    I've tried this...
    function Sidebar() {
    return ($this->Photo || $this->SidebarContent || $this->Menu(2)) ? true : false;
    }

    And a couple of other variations -- all giving errors.

    Thanks,
    Sam

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Conditional Sidebar <% if Menu(2) || Photo || SidebarContent %> doesn't work Link to this post

    What are the errors? That looks correct.

  • Samba Sam
    Avatar
    Community Member
    85 Posts

    Re: Conditional Sidebar <% if Menu(2) || Photo || SidebarContent %> doesn't work Link to this post

    Actually, I was incorrect in that there is no error given in the code:
    function Sidebar() {
    return ($this->Photo || $this->SidebarContent || $this->Menu(2)) ? true : false;
    }

    It just that it always passes "true" even when there is no values for Photo, SidebarContent or Menu(2). As a result the sidebar div still appears on all pages.

    Any more ideas?
    Sam

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Conditional Sidebar <% if Menu(2) || Photo || SidebarContent %> doesn't work Link to this post

    Hi HappySadhu,

    I've had a similar problem when wanting to do certain things if there is no content. I found that the HTML editor adds <p></p> to the field if there was no input.

    If your sidebar content is HTMLText (which i assume it is) it will be returning <p></p> (or even <p><span> </span></p>). So you need to get just the raw text from the HTML content.

    Something like this should work

    function Sidebar() {
    return ($this->Photo || $this->SidebarContent->RAW() || $this->Menu(2)) ? true : false;
    }

    I'm not entirely sure if that syntax is correct, but you get the idea. If that doesn't work, a dirty way to do it is:

    function Sidebar() {
    return ($this->Photo || $this->SidebarContent == '<p></p>' || $this->Menu(2)) ? true : false;
    }

    Basically, you need to dig around and see what $this->SidebarContent is returning and then see if your checking the correct variable. Personally, i would do something like:

    function Sidebar() {
    return ($this->Photo || preg_replace('\s','',$this->SidebarContent->RAW()) || $this->Menu(2)) ? true : false;
    }

    Which simply removes all white space from the plain text version of the HTML content, this means that there must be text in the content.

    Useful link: http://doc.silverstripe.org/doku.php?id=htmltext

    Hope that solves the issue.

  • Samba Sam
    Avatar
    Community Member
    85 Posts

    Re: Conditional Sidebar <% if Menu(2) || Photo || SidebarContent %> doesn't work Link to this post

    Thanks everyone for the input so far,

    So I am able to get the following to work.

    function Sidebar() {
    return ($this->Photo || preg_replace('@<[\/\!]*?[^<>]*?>@si','',$this->SidebarContent)) ? true : false;}

    '@<[\/\!]*?[^<>]*?>@si' strips out all the html tags which leaves just the "raw" text, though $this->SidebarContent !== '<p></p>' works just as well. (The function $this->SidebarContent->RAW() was giving the error "Call to a member function RAW() on a non-object".)

    However, I can't get $this->Menu(2) in the function to work properly as in:
    return ($this->Photo || preg_replace('@<[\/\!]*?[^<>]*?>@si','',$this->SidebarContent) || $this->Menu(2)) ? true : false;}

    $this->Menu(2), similar to issue with SidebarContent in my previous posting, is always passing true even when there is no Menu(2). I've tried doing preg_replace to strip out the html tags but that didn't make any difference.

    Any more suggestions?

    Sam

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Conditional Sidebar <% if Menu(2) || Photo || SidebarContent %> doesn't work Link to this post

    $this->SidebarContent->RAW() was giving the error "Call to a member function RAW() on a non-object"

    To return the Text Object rather then just the data from SidebarContent you might want to try $this->obj('SidebarContent')->RAW()

    1937 Views
Page: 1 2
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.