Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Space character in <% if value = a space %> gives error

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

Page: 1
Go to End
Author Topic: 1862 Views
  • Iain_vdw
    Avatar
    Community Member
    22 Posts

    Space character in <% if value = a space %> gives error Link to this post

    I've been trying to add a class by checking if a value of a certain field has been set to a specified option with the following template code. Only the value that i want to check, contains spaces in the string:

    <% control dataFieldByName(FieldName) %>
    <% if value = a value with spaces %> class="active"<% end_if %>
    <% end_control %>

    This generates the following error:
    Parse error: syntax error, unexpected '}' in <pathname to template on server>

    Is there a way to use space in comparisons in the template? I've tried "a value with a spaces", <% if value = AValueWithSpaces %>, escaping spaces, etc to no avail.

    Any idea how this could be solved, if possible?

  • 3dgoo
    Avatar
    Community Member
    133 Posts

    Re: Space character in <% if value = a space %> gives error Link to this post

    Insanely, I don't think this is possible in the template.

    You'll need write a function in your php class and call that.

  • Blake_NiteoDesign
    Avatar
    Community Member
    17 Posts

    Re: Space character in <% if value = a space %> gives error Link to this post

    Seriously?

    Came here hoping for an answer to this very query and... fail.

    Where might I go to find out how to add the function properly? Thanks!

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Space character in <% if value = a space %> gives error Link to this post

    All you need to do is create a function in the controller for the page type in question.

    Controller:

    function getActive() {
    if ($this->FieldName == 'a value with spaces') {
    return true;
    }
    }

    Template:

    <% if Active %> class="active"<% end_if %>

    Enjoy!

    NB: It is important to realise that the templating language is designed to be SIMPLE, this reduces parsing time and enforces a clear separation of controller and view, which is the whole reason we are using a beautiful MVC framework like SilverStripe

  • Iain_vdw
    Avatar
    Community Member
    22 Posts

    Re: Space character in <% if value = a space %> gives error Link to this post

    NB: It is important to realise that the templating language is designed to be SIMPLE, this reduces parsing time and enforces a clear separation of controller and view, which is the whole reason we are using a beautiful MVC framework like SilverStripe

    True, but writing custom php functions beats the purpose of an easy templating language as imho the easy templating system shoud allow check for variables with a space. Not everyone knows php as much as some of us do.

  • Capt. Morgan
    Avatar
    Community Member
    30 Posts

    Re: Space character in <% if value = a space %> gives error Link to this post

    I agree with Pigeon on this. Even if it seems the templates should be able to read the string with blankspace in it. I would rather keep the template as clean of the comparison operators as far as possible.

  • Blake_NiteoDesign
    Avatar
    Community Member
    17 Posts

    Re: Space character in <% if value = a space %> gives error Link to this post

    Pigeon, Thanks! You rock!

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