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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

Active link


Go to End


5 Posts   3362 Views

Avatar
Lee

Community Member, 6 Posts

9 February 2010 at 11:32pm

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.

Avatar
bummzack

Community Member, 904 Posts

10 February 2010 at 2:08am

Edited: 10/02/2010 2:09am

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

Avatar
Lee

Community Member, 6 Posts

10 February 2010 at 2:23am

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?

Avatar
bummzack

Community Member, 904 Posts

10 February 2010 at 4:39am

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

Avatar
Lee

Community Member, 6 Posts

10 February 2010 at 5:01am

Edited: 10/02/2010 5:02am

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!