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


Go to End
Reply


15 Posts   2196 Views

Avatar
banal

Community Member, 901 Posts

24 August 2009 at 6:15pm

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

Community Member, 243 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
Pigeon

Community Member, 243 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
Pigeon

Community Member, 243 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