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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

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


Go to End


7 Posts   3121 Views

Avatar
Iain_vdw

Community Member, 22 Posts

23 July 2010 at 1:07am

Edited: 23/07/2010 1:08am

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

Community Member, 135 Posts

27 July 2010 at 11:44am

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

Community Member, 17 Posts

28 July 2010 at 8:22am

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
dhensby

Community Member, 253 Posts

28 July 2010 at 9:06am

Edited: 28/07/2010 9:08am

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

Community Member, 22 Posts

29 July 2010 at 1:58am

Edited: 29/07/2010 1:58am

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

Community Member, 30 Posts

29 July 2010 at 2:59am

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

Community Member, 17 Posts

21 August 2010 at 4:51am

Pigeon, Thanks! You rock!