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

<% if Link == "/" %> not work


Go to End


12 Posts   2834 Views

Avatar
Josua

Community Member, 87 Posts

8 March 2012 at 8:55am

Edited: 08/03/2012 9:13am

Hi all!

In template the following not work:

<% if Link == "/" %>

I get the following error when I do http://127.0.0.1/?flush=all:
Parse error: syntax error, unexpected '}' in C:\wamp\www\silverstripe-cache\.cache.themes.mysite.templates.Page.ss on line 406

How should I put this comparison in the template?

Thanks,
Regards,
Jose

Avatar
swaiba

Forum Moderator, 1899 Posts

8 March 2012 at 9:13am

You shouldn't make a function in the controller and use that.

Avatar
Josua

Community Member, 87 Posts

8 March 2012 at 9:31am

Edited: 08/03/2012 9:34am

Hi Barry!

But, in my Navigation.ss
if I want to do the following:

<ul>
<% control Menu(1) %>
<% if Link == "/" %>
<li><a href="$Link?locale=$locale" title="Go to the $Title.XML page" class="$LinkingMode"><span>$MenuTitle.XML</span></a></li>
<% else %>
<li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode"><span>$MenuTitle.XML</span></a></li>
<% end_if %>
<% end_control %>
</ul>

Can't I do this?

How could I do this?

What am I doing wrong?

Thanks Barry!

Regards,
Jose

Avatar
Juanitou

Community Member, 323 Posts

8 March 2012 at 11:27am

Edited: 08/03/2012 11:29am

Hi Jose,

What you are doing wrong is not reading the documentation, look here:
http://doc.silverstripe.org/sapphire/en/reference/advanced-templates

In the section “If blocks” of this page you’ll find an explanation of how to use those if conditions, that are not as rich as PHP ones, for example: You should not include quotes around the value. Also, / will not work, I guess you should use home:
<% if Link == home %>

Hope it helps,
Juan

Avatar
Josua

Community Member, 87 Posts

8 March 2012 at 8:37pm

Edited: 08/03/2012 8:39pm

Hi Juanitou!

Yes, I read the documentation and I did that test ( <% if Link == home %> ), but it did not work. This condition does not occur, for this reason I tried the other type of condition:
<% if Link == "/" %>

No one of the 2 ways works.

This way ( <% if Link == home %> ) does not produce any errors, but the condition is not true. Why?

I no longer know what to try.

Please some help.

Thanks,
Regards,

Jose

Avatar
Juanitou

Community Member, 323 Posts

8 March 2012 at 9:29pm

Hi!

Don’t use Link:

<% if URLSegment == home %>

I should have spotted it before, sorry.

Hope it helps,
Juan

Avatar
swaiba

Forum Moderator, 1899 Posts

8 March 2012 at 9:43pm

Just to be clear... I should have given a slightly longer response...

You shouldn't [use an if for that]

[instead ]make a function in the controller

function LinkIsForwardSlash() {
return ($this->Link() == '/');
}

and use that in the template. You will be able to use exact PHP to solve the problem and Debug::show() will work so you can figure it all out.

Avatar
Josua

Community Member, 87 Posts

9 March 2012 at 12:59am

Hi Juanitou!

Yes, Yes, Yes!!!! :)
It works!!!
The trick was to use URLSegment. :)

Thank you very much.

Regards,
Jose

Go to Top