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   3748 Views

Avatar
bummzack

Community Member, 904 Posts

24 August 2009 at 6:15pm

Or simply use a combination of strip_tags and trim?

trim( strip_tags( $this->SidebarContent ) );

Avatar
dhensby

Community Member, 253 Posts

24 August 2009 at 11:33pm

I've read bad things about strip_tags not working properly, i would use the inbuilt SS function (havent investigated how it works) and removing white space. In conjunction with the RAW version of the content.

Avatar
Samba Sam

Community Member, 85 Posts

25 August 2009 at 12:40pm

Hi all,

Any thoughts on how to get the Menu(2) part of the Sidebar function working, which is really the only remaining issue (see my previous post)?
It is always passing true even when there is no Menu(2) items.

Sam

Avatar
dhensby

Community Member, 253 Posts

25 August 2009 at 11:39pm

Debug what Menu(2) actually returns. There may be a count number or a total number of items stored with in the object and you can check to see if that is 0.

Avatar
Samba Sam

Community Member, 85 Posts

26 August 2009 at 4:46am

My knowledge of php is fairly poor, so I am not sure how I would determine what Menu(2) returns.
What and where would be the code that I would use?

Thanks,
Sam

Avatar
dhensby

Community Member, 253 Posts

26 August 2009 at 7:13am

Look into the silverstripe Debug module. Something like

Debug::show(Menu(2));

Should show everything you need.

Avatar
Samba Sam

Community Member, 85 Posts

5 September 2009 at 11:40am

Edited: 09/12/2009 9:18am

I am happy to report that from the suggestions above and from the IRC channel, I was able to complete the code I was looking for.

Its... function Sidebar() { return ($this->Photo()->exists() || strip_tags($this->SidebarContent) || $this->Menu(2)->Count() > 0);}

The ternary operators ? true : false; at the end are not necessary. it's either true or false already, no need for an extra check.
The strip_tags seems to work fine for checking for raw text. It doesn't return true if there are only spaces in the htmltext field. I couldn't get Will's suggestion $this->obj('SidebarContent')->RAW() to work.
The Debug::show(Menu(2)); didn't show me default output to use in function -- at least I couldn't figure it out from what was shown.

Thanx all again for the help!
Sam

Edit: See http://www.silverstripe.org/template-questions/show/273271#post273271 for conditional if statements regarding images. I updated
the above by changing $this->Photo to $this->Photo()->exists as just $this->Photo was returning as a false true.

Go to Top