Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » double IF conditions

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

Page: 1
Go to End
Author Topic: 7563 Views
  • Entar
    Avatar
    Community Member
    19 Posts

    double IF conditions Link to this post

    Hi everyone,

    I have a big problem with double if conditions, when I try to use something like

    <% if ClassName != ForumHolder && ClassName != Forum %>
    do something
    <% else %>
    blablabla
    <% end_if %>

    or

    <% if (ClassName != ForumHolder) && (ClassName != Forum) %>
    do something
    <% else %>
    blablabla
    <% end_if %>

    or other variants with quotes, double quotes or any without brackets - I have an error like

    syntax error, unexpected '}' in ...

    Is it unreal to use double conditions in templates or there is something I don't know about? How can I solve this?

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: double IF conditions Link to this post

    The template syntax is pretty basic and can't do what you're after.

    Scroll down on this page to see http://doc.silverstripe.com/doku.php?id=templates

    I'd suggest using else_if or nesting them if possible. Hackish I know, but that's about it.

  • Entar
    Avatar
    Community Member
    19 Posts

    Re: double IF conditions Link to this post

    Yeah, I done this before, but it's very ugly! Btw - there IS example of double conditions on wiki page, but it's just not working

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: double IF conditions Link to this post

    I think the only double if that works (at least it did once ;) ) is in the form of:

    <% if Property || Property2 %> or <% if Property && Property2 %>

    so not when evaluating them against other values.

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: double IF conditions Link to this post

    Yes as dio5 said, it won't work with multiple expressions when evaluating against values, what I do is make functions in the controller to return true or false example:

    Controller:

    public function IsForumSection()
    {
    switch($this->ClassName) {
    case 'ForumHolder':
    case 'ForumPage':
    return true;
    break;
    default:
    return false;
    }
    }

    Template:

    <% if IsForumSection %>
    do something
    <% else %>
    blablabla
    <% end_if %>

    This way you can call multiple expressions and have the logic as complex as you like...

  • Entar
    Avatar
    Community Member
    19 Posts

    Re: double IF conditions Link to this post

    Thanks! Thats great.

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: double IF conditions Link to this post

    I have seen this in the wiki:
    <% if Property %>
    ... optional content ...
    <% else_if OtherProperty %>
    ... alternative content ...
    <% else %>
    ... alternative content ...
    <% end_if %>

    So I tried this:

    <% control Top %>
    <% if InSection(section1) %>
    <% include File1 %>
    <%else_if InSection(section2) %>
    <% include File2 %>
    <% else %>
    <% include File3 %>
    <% end_if %>
    <% end_control %>

    However the esle_if does not seem to work. Instead if the first one evaluates to true, the <% else_if InSection(section2) %> gets printed in the output as a string after the file is included. If the first one is false and the second one is supposed to be true the last file gets included.

    I am using the Trunk version of 2.3 downloaded via SVN 2/5/2009.

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: double IF conditions Link to this post

    Your logic must be in the controller and not the template, also you cannot pass attributes to the template functions that way just make separate functions in the controller.

    Another option is to use your page types as in ClassName == 'NewsPage' etc.... this does work

    HTH

    7563 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.