Jump to:

23453 Posts in 18902 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Active link

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1961 Views
  • Lee
    Avatar
    Community Member
    6 Posts

    Active link Link to this post

    Hi, I'm tying to get my navigation menu to highlight which page you are on using this code

    <ul id="menu">
          <% control getTopNav %>
                   <% if First %>
                      <li class="first<% if LinkorSection = section %> active<% end_if %>"><a href=$Link>$MenuTitle</a></li>
                   <% else_if Last %>
                      <li class="last"><a href=$Link>$MenuTitle</a></li>
                   <% else %>
                      <li><a href=$Link>$MenuTitle</a></li>
                   <% end_if %>
                <% end_control %>
    </ul>

    It highlight the hompage when your on that page but not on any others??

    Any ideas would be great, thanks.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Active link Link to this post

    Hi
    In your code you're only highlighting the first item, since you only apply the "active" class in the <% if First %> block.

    The whole code can be written much much simpler, like this:

    <ul id="menu">
       <% control getTopNav %>
       <li class="$FirstLast $LinkOrSection"><a href=$Link>$MenuTitle</a></li>
       <% end_control %>
    </ul>

    Because $FirstLast will output "first" for the first item, and "last" for the last item.. no need for the if-else blocks. Same goes for $LinkOrSection. It will either output "link" or "section" (see here for a reference of these controls: http://doc.silverstripe.org/doku.php?id=built-in-page-controls).
    The only thing you need to do is change your stylesheet from .active to .section

  • Lee
    Avatar
    Community Member
    6 Posts

    Re: Active link Link to this post

    Thanks for the reply,

    I just swapped that code for mine and changed the class in the stylesheet but seem to get the same effect?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Active link Link to this post

    Did you try flushing the template-cache of the page? (by appending ?flush=1 to the URL)

  • Lee
    Avatar
    Community Member
    6 Posts

    Re: Active link Link to this post

    I tried flushing it, the funny thing is that now it's suddenly highlighting on the homepage and the news pages but none of the others.

    EDIT -

    I had only flushed the news page and not the rest, now i am they seem to be working too!

    Thanks for the help!

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