Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Difficulties with If-Statements

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

Page: 1
Go to End
Author Topic: 3704 Views
  • John Silver
    Avatar
    Community Member
    10 Posts

    Difficulties with If-Statements Link to this post

    I tried to give all pages that don't have any children the class attribute "leaf".

    After experimenting quite some time with this:

    <div id="page"<% if Children.TotalItems < 1 %> class="leaf"<% end_if %>>
    or
    <div id="page"<% if !Children %> class="leaf"<% end_if %>>
    or
    <div id="page"<% if Children.TotalItems == 0 %> class="leaf"<% end_if %>>

    which all gave a "Parse error", I finally had to use this very odd method to get it to work:

    <div id="page"<% if Children.TotalItems %><% else %> class="leaf"<% end_if %>>

    This doesn't seem right. What did I do wrong? There must be an easier way to accomplish this.
    Any thoughts? Is it not possible to do comparisons in Silverstripe templates?

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Difficulties with If-Statements Link to this post

    Hi John,

    The template language isn't like PHP, so doing:

    <div id="page"<% if Children.TotalItems = 0 %> class="leaf"<% end_if %>>

    Should work (note only ONE '=')

    Hope that helps!

  • John Silver
    Avatar
    Community Member
    10 Posts

    Re: Difficulties with If-Statements Link to this post

    Thanks for the reply, but that doesn't work either. It doesn't throw a parse error though - the statement is simply ignored.

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Difficulties with If-Statements Link to this post

    So if ... = 0 and if != 0 are both ignored?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Difficulties with If-Statements Link to this post

    Try <% if Children %><% else %>class="leaf"<% end_if %> which should work. Saying that I would have thought Children.TotalItems would have worked.

    Just to clear up the others

    <% if !Children %> Will not work - negation is not supported
    <% if Children.TotalItems == 0 %> Will not work. SSViewer uses = not == as it doesn't have assignment.

  • John Silver
    Avatar
    Community Member
    10 Posts

    Re: Difficulties with If-Statements Link to this post

    My solution now:

    I put this in my page class

       public function isLeaf() {
          return $this->Children()->TotalItems() == 0;
       }

    and this in my page.ss:

    <div id="page"<% if isLeaf %> class="leaf"<% end_if %>>

    Now the code looks clean. I'm not sure that this is the "right" way to do it, as I'm not really an expert with this MVC thing. Should this be in the Controller or somewhere else?

    I still think negations and comparisons should be possible in the template. Is this just not the way things are done with SilverStripe or will this functionality be added in the future?
    It seems very odd to me to always write something like

    <% if Control %><% else %>Output<% end_if %>

    instead of

    <% if !Control %>Output<% end_if %>

    Oh and for the

    <% if Children.TotalItems = 0 %> class="leaf"<% end_if %>


    - tried it again to make sure. Doesn't work.

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