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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

double IF conditions


Go to End


8 Posts   13869 Views

Avatar
Entar

Community Member, 23 Posts

25 December 2008 at 3:57am

Edited: 25/12/2008 3:59am

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?

Avatar
Liam

Community Member, 470 Posts

25 December 2008 at 6:04am

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.

Avatar
Entar

Community Member, 23 Posts

25 December 2008 at 6:50am

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 :(

Avatar
dio5

Community Member, 501 Posts

27 December 2008 at 10:55pm

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.

Avatar
CodeGuerrilla

Community Member, 105 Posts

5 January 2009 at 6:49pm

Edited: 05/01/2009 7:51pm

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

Avatar
Entar

Community Member, 23 Posts

18 January 2009 at 2:00am

Thanks! Thats great.

Avatar
micahsheets

Community Member, 165 Posts

6 February 2009 at 1:46pm

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.

Avatar
CodeGuerrilla

Community Member, 105 Posts

6 February 2009 at 1:56pm

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