Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » How to test 'if not' in template?

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

Page: 1
Go to End
Author Topic: 4255 Views
  • Romeo
    Avatar
    Community Member
    67 Posts

    How to test 'if not' in template? Link to this post

    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.

  • Double-A-Ron
    Avatar
    Community Member
    599 Posts

    Re: How to test 'if not' in template? Link to this post

    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

  • Romeo
    Avatar
    Community Member
    67 Posts

    Re: How to test 'if not' in template? Link to this post

    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.

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: How to test 'if not' in template? Link to this post

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

  • Romeo
    Avatar
    Community Member
    67 Posts

    Re: How to test 'if not' in template? Link to this post

    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

  • Double-A-Ron
    Avatar
    Community Member
    599 Posts

    Re: How to test 'if not' in template? Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: How to test 'if not' in template? Link to this post

    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

    4255 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.