Jump to:

10979 Posts in 2722 Topics by 1815 members

All other Modules

SilverStripe Forums » All other Modules » Conditional logic

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1191 Views
  • Junglefish
    Avatar
    Community Member
    104 Posts

    Conditional logic Link to this post

    What's wrong with this code?

                   <% if InSection(administrative-news) %>
                      administrative-news
                   <% else_if InSection(image-galleries) %>
                      image-galleries
                   <% else %>
                      ... alternative content ...
                   <% end_if %>

    When the first conditional is true, this is what prints to the browser:
    "administrative-news <% else_if InSection(image-galleries) %> image-galleries"

    When the second conditional is true, this is what prints to the browser:
    "... alternative content ..."

    It's like the "else_if" clause is being interpreted as a string(!) What am I doing wrong?

    jf/

  • datswicked
    Avatar
    Community Member
    15 Posts

    Re: Conditional logic Link to this post

    I believe you have to nest your if's as they don't work multi level (if, else if, else if, else) like php, only support 1 level (if, else) in templates..

    ie i have done this as part of a product template and it works ok, bit of a hack but it works..

    <% if Level(5) %>
       <!-- 5: PRODUCT -->
       <% include prod_aCatProd %>
    <% else %>
       <% if Level(4) %>
          <!-- 4: PRODUCT LISTING.. -->
          <% include prod_aCategory4 %>
       <% else %>
          <% if Level(3) %>
             <!-- 3: STORAGE -->
             <% include prod_aCategory3 %>
          <% else %>
             <% if Level(2) %>
                <!-- 2: LANDING -->
             <% end_if %>
          <% end_if %>   
       <% end_if %>
    <% end_if %>

    so you should have

    <% if InSection(administrative-news) %>
    administrative-news
    <% else %>
    <% if InSection(image-galleries) %>
    image-galleries
    <% else %>
    ... alternative content ...
    <% end_if %>
    <% end_if %>

    then it will check the admin news first then the image galleries and then alternative content last

    hope that helps. I know this is an old post but i came across it with no answer and hopefully it helps someone..

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