Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

Space character in <% if value = a space %> gives error


Reply

7 Posts   1871 Views

Avatar
Iain_vdw

23 July 2010 at 1:07am (Last edited: 23 July 2010 1:08am), Community Member, 22 Posts

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?

Avatar
3dgoo

27 July 2010 at 11:44am Community Member, 133 Posts

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

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

Avatar
Blake_NiteoDesign

28 July 2010 at 8:22am Community Member, 17 Posts

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!

Avatar
Pigeon

28 July 2010 at 9:06am (Last edited: 28 July 2010 9:08am), Community Member, 243 Posts

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 :)

Avatar
Iain_vdw

29 July 2010 at 1:58am (Last edited: 29 July 2010 1:58am), Community Member, 22 Posts

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.

Avatar
Capt. Morgan

29 July 2010 at 2:59am Community Member, 30 Posts

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.

Avatar
Blake_NiteoDesign

21 August 2010 at 4:51am Community Member, 17 Posts

Pigeon, Thanks! You rock!