Auto Navigation Highlighting in ExpressionEngine
We’ve been using this technique for a while now and it works great! We always take the most used parts of a site and put them in templates for easy access when putting together a site. But what happens when you want to highlight the navigation to show the user where they’re at? Enter this cool trick! And yes, you have to be using ExpressionEngine .
Alright, say this is your navigation code:
<ul> <li><a class="current" href="./">Home</a></li> <li><a href="dont_hire/">Don’t Hire Us</a></li> <li><a href="portfolio/">Portfolio</a></li> </ul>
Notice the current
class. I’ve styled that in my CSS to show that I’m on that page (for this site it would be white with a star to the left). I’m going to use Expression Engine’s if
and loc
tags to automate the process. In my navigation template, I setup the code like this:
<ul> <li><a {if ‘{embed:loc}’ == ‘home’}class="current"{/if} href="./">Home</a></li> <li><a {if ‘{embed:loc}’ == ‘dont_hire’}class="current"{/if} href="dont_hire/">Don’t Hire Us</a></li> <li><a {if ‘{embed:loc}’ == ‘portfolio’}class="current"{/if} href="portfolio/">Portfolio</a></li> </ul>
The name after the double equal signs should quickly describe what page it is. Also, you can move the entire if
expression around; for example if it was the class you wanted to change instead of the
class. So once this is set up, we’re going to use the “Don’t Hire” page as an example. We need to embed the navigation template like normal, but add one small addition:
{embed="global/nav" loc="dont_hire"}
Yep, that loc=
is all you need. Just change what’s in the quotation marks depending on what page you’re on and match it to what you put after the double equal sign up above. This is great because now we can call that same navigation all over the site and have one easy place to update it without breaking out current
classes. Here’s the rendered code:
<ul> <li><a href="./">Home</a></li> <li><a class="current" href="dont_hire/">Don’t Hire Us</a></li> <li><a href="portfolio/">Portfolio</a></li> </ul>
Hope you find this useful. I sure do.
Comments
1
Tim Van Damme - Jan 30, 2008
2
tzEEstuff - Jan 31, 2008
3
Ty - Jan 31, 2008
Jonathan Longnecker - Feb 09, 2008
5
Richard Angstmann - Mar 27, 2008
Jonathan Longnecker - Mar 28, 2008
7
Richard Angstmann - Mar 28, 2008
Jonathan Longnecker - Mar 28, 2008
9
Richard Angstmann - Mar 28, 2008
Jonathan Longnecker - Mar 28, 2008
11
N8 - May 29, 2008
Jonathan Longnecker - May 30, 2008
13
Clyde Hill - Jun 12, 2008
Jonathan Longnecker - Jun 13, 2008
15
swim - Jun 14, 2008
16
forex - Jul 01, 2008
17
buy headlight cleaner - Jul 16, 2008
18
Dan - Oct 30, 2008
19
diana - Jul 13, 2009
Jonathan Longnecker - Jul 13, 2009
21
Brent - Oct 01, 2009
22
Raul Ferrer - Oct 10, 2009
23
CJ - Feb 17, 2010
24
Michael Kitchens - May 19, 2012