Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Has else_if been removed from the template language?

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

Page: 1
Go to End
Author Topic: 2079 Views
  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Has else_if been removed from the template language? Link to this post

    I found an old post about control structures in templates that states that else_if should work but when I try to use it in my templates it just gets printed as raw text.

    I tried doing something like this

    <% if Level(2) %>
    $Parent.Title
    <% else_if Level(3) %>
    $Title
    <% else %>
    $Title
    <% end_if %>


    But I get the else_if back in raw text.

  • netnerd85
    Avatar
    Community Member
    42 Posts

    Re: Has else_if been removed from the template language? Link to this post

    I'm having the same problem, does else_if work or not? What's the deal with it just coming out as plain text? My code is:

    <% if InSection(home) %>
    home
    <% else_if InSection(about) %>
    about
    <% else %>
    subpage
    <% end_if %>

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Has else_if been removed from the template language? Link to this post

    Did this ever get answered? I have the same problem, see here: http://www.silverstripe.org/all-other-modules/show/293853?showPost=293853

    jf/

  • swaiba
    Avatar
    Forum Moderator
    1786 Posts

    Re: Has else_if been removed from the template language? Link to this post

    I'd raise this as a bug as I've seen it too!

    But it is soooo simple to fix that I hadn't bothered...

    <% if InSection(home) %>
       home
    <% else %>
       <% if InSection(about) %>
          about
       <% else %>
          subpage
       <% end_if %>
    <% end_if %>

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Has else_if been removed from the template language? Link to this post

    @Swaiba

    That's all well and good if you only have 3 cases. Unfortunately I have about 8 which leads to a very messy set of nested conditionals which goes something like this:

    <% if InSection(home) %>
    home
    <% else %>
       <% if InSection(about) %>
          about
       <% else %>
          <% if yyyyyyyyyyyyyyyyyyyy %>
             subpage
          <% else %>
             <% if xxxxxxxxxxxxxx %>
                   something else
             <% else %>
                <% if zzzzzzzzzzzzzzzzz %>
                      something else
                   <% else %>
                      something else
                <% end_if %>
             <% end_if %>
          <% end_if %>
       <% end_if %>
    <% end_if %>

    And that's the simplified version(!)

    So, does anyone know, has "else_if" actually been removed???

    jf/

  • swaiba
    Avatar
    Forum Moderator
    1786 Posts

    Re: Has else_if been removed from the template language? Link to this post

    Oh Absolutely Junglefish!

    As I said "I have noticed else_if not working as well" and if you want to get an official response then it is always easier to go to http://open.silverstripe.org/ and raise a ticket!

    IMHO if you have such a large complex if / else if I'd think it was too complex and try and achieve it slightly differently using different page types, includes or involving the controller a little more. Or you could do the following...

    <% if InSection(home) %>
    home
    <% end_if %>
    <% if InSection(about) %>
    about
    <% end_if %>
    <% if yyyyyyyyyyyyyyyyyyyy %>
    subpage
    <% end_if %>
    etc..

    or without the formating if you have a small amount of code inside each if (and if not, perhaps including templates might help)...

    <% if InSection(home) %>home<% end_if %>
    <% if InSection(about) %>about<% end_if %>
    <% if yyyyyyyyyyyyyyyyyyyy %>subpage<% end_if %>
    etc..

    And yes that also has some flaws... Personally I'd recommend trying to do this work without such a large/complex logic in the template. There are many other ways to do things in silverstripe, as yet another suggestion you could extend page to add to the $db a field MyCustomText and then for each of these page types you can edit the text in the cms and then you only need $MyCustomText in the template.

    Just to make it clear I agree that else_if is buggy, but I am also suggesting that there are other, perhaps better ways, to get the same result.

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Has else_if been removed from the template language? Link to this post

    <% else_if %> has not been removed, it just doesn't accept as much options as <% if %> does. The valid ways of using <% else_if %> are:

    • <% else_if Val %>
    • <% else_if Val1 || Val2 %>
    • <% else_if Val1 && Val2 %>
    • <% else_if Val1 == Val2 %>
    • <% else_if Val1 != Val2 %>
    • <% else_if Obj.Val %>
    • <% else_if Obj.Meth.Val %>

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