Jump to:

3447 Posts in 1032 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » <% if Link == "/" %> not work

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

Page: 1 2
Go to End
Author Topic: 1092 Views
  • Josua
    Avatar
    Community Member
    86 Posts

    <% if Link == "/" %> not work Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: <% if Link == "/" %> not work Link to this post

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

  • Josua
    Avatar
    Community Member
    86 Posts

    Re: <% if Link == "/" %> not work Link to this post

    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

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: <% if Link == "/" %> not work Link to this post

    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

  • Josua
    Avatar
    Community Member
    86 Posts

    Re: <% if Link == "/" %> not work Link to this post

    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

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: <% if Link == "/" %> not work Link to this post

    Hi!

    Don’t use Link:

    <% if URLSegment == home %>

    I should have spotted it before, sorry.

    Hope it helps,
    Juan

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: <% if Link == "/" %> not work Link to this post

    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.

  • Josua
    Avatar
    Community Member
    86 Posts

    Re: <% if Link == "/" %> not work Link to this post

    Hi Juanitou!

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

    Thank you very much.

    Regards,
    Jose

    1092 Views
Page: 1 2
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.