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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

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


Go to End


15 Posts   3749 Views

Avatar
Samba Sam

Community Member, 85 Posts

20 August 2009 at 6:22am

Edited: 24/08/2009 11:57am

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

Avatar
Willr

Forum Moderator, 5523 Posts

20 August 2009 at 12:17pm

Edited: 20/08/2009 12:18pm

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;
}

Avatar
Samba Sam

Community Member, 85 Posts

21 August 2009 at 11:06am

Edited: 21/08/2009 11:49am

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

Avatar
Willr

Forum Moderator, 5523 Posts

21 August 2009 at 12:08pm

What are the errors? That looks correct.

Avatar
Samba Sam

Community Member, 85 Posts

22 August 2009 at 8:51am

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

Avatar
dhensby

Community Member, 253 Posts

22 August 2009 at 10:33am

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.

Avatar
Samba Sam

Community Member, 85 Posts

24 August 2009 at 11:54am

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

Avatar
Willr

Forum Moderator, 5523 Posts

24 August 2009 at 1:06pm

$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()

Go to Top