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

Trouble with If block syntax in templates


Go to End


2 Posts   2802 Views

Avatar
scpi

Community Member, 21 Posts

1 October 2009 at 7:25am

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"

Avatar
Willr

Forum Moderator, 5523 Posts

1 October 2009 at 6:55pm

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.