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

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


Reply

15 Posts   2087 Views

Avatar
banal

24 August 2009 at 6:15pm Community Member, 901 Posts

Or simply use a combination of [url=http://php.net/manual/en/function.strip-tags.php]strip_tags[/url] and [url=http://php.net/manual/en/function.trim.php]trim[/url]?

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

Avatar
Pigeon

24 August 2009 at 11:33pm Community Member, 243 Posts

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

25 August 2009 at 12:40pm Community Member, 85 Posts

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
Pigeon

25 August 2009 at 11:39pm Community Member, 243 Posts

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

26 August 2009 at 4:46am Community Member, 85 Posts

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
Pigeon

26 August 2009 at 7:13am Community Member, 243 Posts

Look into the silverstripe Debug module. Something like

Debug::show(Menu(2));

Should show everything you need.

Avatar
Samba Sam

5 September 2009 at 11:40am (Last edited: 9 December 2009 9:18am), Community Member, 85 Posts

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.