Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » How to handle space in if string?

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

Page: 1
Go to End
Author Topic: 2094 Views
  • 3 midgets In a man suit
    Avatar
    Community Member
    13 Posts

    How to handle space in if string? Link to this post

    Here is the basic situation:

    I am trying to figure out how to use a value that has a space in it for an if statement

    example:

    <% if ProductGroup = Branson Shows %>
    <% end_if %>

    if the value has no space it works fine i.e.

    <% if ProductGroup = Hotels %>
    <% end_if %>

    but I have a few key values that have spaces in between them

    any help would be appreciated

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: How to handle space in if string? Link to this post

    If I'm correct, in the latest versions you can use quotes "" and ==, but I don't think spaces are allowed (yet). (Correct me if I'm wrong someone)

    An option is to work around it in the controller, where you could iterate over it and add a hyphen or something to it.

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: How to handle space in if string? Link to this post

    If you delimit the space with a \ does that work?

  • 3 midgets In a man suit
    Avatar
    Community Member
    13 Posts

    Re: How to handle space in if string? Link to this post

    I've tried the / and hyphen to delimit the space with no luck.

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: How to handle space in if string? Link to this post

    I've confirmed that you can't currently use spaces in template comparisons, delimited or otherwise (for 2.3.3).

    They generate template code like this

    if($item->XML_val("Pos",null,true) == "24") { ;

    Any spaces, including delimited will produce static text

    $val .= <<<SSVIEWER
    <% Pos == "2 4" %>

    You can try to modify your names or just group your output differently. Sorry in advanced, I know this is a pretty frustrating limitation in Template Control blocks. I've worked around it before with a control that had a switch/case in it to return a boolean.

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: How to handle space in if string? Link to this post

    Have you tried?

    <% if ProductGroup = BransonShows %>
    <% end_if %>

    Just removing has worked for me in other controls/if statements

    Otherwise use the URL segment

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