Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Trouble with If block syntax in templates

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

Page: 1
Go to End
Author Topic: 1977 Views
  • scpi
    Avatar
    Community Member
    19 Posts

    Trouble with If block syntax in templates Link to this post

    I've run into a confusing issue with template syntax, maybe somebody can help me out. The following two snippets seem like they should be equivalent, but apparently they're not. There must be a reason why, but I have no idea what it could be. Any thoughts?

    In a template, this works:

    <% control Parent %>
    <% if URLSegment == home %>
       <p>foo</p>
    <% end_if %>
    <% end_control %>

    But this does not:

    <% if Parent.URLSegment == home %>
       <p>foo</p>
    <% end_if %>

    The second example returns a blank page and logs this error:

    "PHP Parse error: syntax error, unexpected '}' in /private/var/folders/HJ/HJPYl5CrFjSn7+gK6JPhl2pKtAE/-Tmp-/silverstripe-cache-Applications-MAMP-htdocs/.cache.Applications.MAMP.htdocs.themes.mytheme.templates.Page.ss on line 264"

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Trouble with If block syntax in templates Link to this post

    One note - I didn't think the template worked with == thought it needed to be =.

    I think that this is a limitation with the parser. Its not able to evaluate Parent.URLSegment *then* do the comparison, it can only evaluate once per statement. You are probably best doing it with the control then the if.

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