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 /

How to test 'if not' in template?


Reply


7 Posts   5044 Views

Avatar
Romeo

Community Member, 67 Posts

25 November 2009 at 9:03am

One thing I find myself constantly needing is a way of doing an 'if not' test in templates, not just an 'if' test. I know that something like <% if ! whatever %> or <% if not whatever %> is not valid, but I'm sure there must be some way round this which people use. The only solution I have at the moment is to have a positive test then stick the code I want to run in the <% else_if ... %> branch, which is inelegant to say the least. What do more experienced SilverStripers do?

Incidentally, I'm sorry if this has come up a lot before (I'm sure it must have), but it's a very difficult one to search for because of the nature of the keywords.

Avatar
Double-A-Ron

Community Member, 604 Posts

25 November 2009 at 5:24pm

Well, it really depends on what you are checking for. You can use != in templates for simple text conditionals.

<% if ClassName != MyPageType %>
   This is not a MyPage page
<% end_if %>

In my case, if I need anything more complex than that, it belongs in the controller anyway, not the template. Like So:

Controller:

function notTotallyCool(){
   if($this->CoolPoints < 50) {
      return true;
   } else {
      return false;
   }
}

Template:

<% if notTotallyCool %>
   Sorry, you aren't cool enough to see this page.
<% end_if %>

Bad example I know, but in a nutshell, if there is any form of condition that is beyond the simple, I generally put the logic in the controller and access it using one call on the template.

The reason is because I've learned that it comes in handy when trying to track down logic bugs. I don't need to sift through both the template and Controller when I can't remember where the logic is done.

Aaron

Avatar
Romeo

Community Member, 67 Posts

25 November 2009 at 10:32pm

Really? I'm amazed you can do this:

<% if ClassName != MyPageType %>

I have been labouring long under the evident misconception that != did not work in templates. Is this something which is relatively recently added? I'm sure I've tested this a while back and run into syntax errors. I guess it's time to have another play! Thanks.

Avatar
Martijn

Community Member, 271 Posts

26 November 2009 at 6:28am

I never got != either in templates....

Avatar
Romeo

Community Member, 67 Posts

26 November 2009 at 6:48am

I just tested it and it worked. It's strange because I'm sure it hasn't worked in the past and I know I've read more than once that it doesn't work. This, for example, from Willr, who is definitely an expert, where he says "Negation is not supported currently by SS so you have to do a bit of a work around":

http://www.silverstripe.org/general-questions/show/264755#post264755

Avatar
Double-A-Ron

Community Member, 604 Posts

26 November 2009 at 8:56am

I am unsure when it was added, but you would be forgiven for thinking it didn't work. I didn't know this until I read the SS book.

Will knows his stuff. It may be something that was added since July when he posted it that, however I can't find a mention in 2.3.1, 2.3.2 or 2.3.3 changelogs.

The book is based on 2.3.3.

Aaron

Avatar
Willr

Forum Moderator, 5513 Posts

26 November 2009 at 1:22pm

Hmm looking at svn blame - I see != was added over 3 years ago so ClassName != HomePage is not a 'new' thing. It seems like its something that has always been there in fact. If it works then use it :)