Best way to manage header navigation menu from template?
I am looking to put navigation in my GSP template and I would like to set a class active
for navigation elements for each respective page. What's the best way to do this? I have multiple views .gsp
combined with one template that looks like this:
<div id="bd" role="main">
<div role="navigation" class="yui-g">
<ul id="nav"><a href="index.gsp"><li class="active">Home</li></a><a href = "products.gsp"><li>Products</li></a><a href = "contacts.gsp"><li>Contact</li></a></ul>
</div>
<g:layoutBody/>
</div>
a source to share
I like armandino's suggestion, however you may have problems if you access the pages in other ways than by clicking on the menu (for example, via a bookmark or the first page after logging in).
This is a different solution if you are using SiteMesh, however it is not isolated from the menu template and therefore not as good in design:
a source to share
This is usually done by passing in a parameter, let's call it activeView
. Then in your menu template you can check which menu item to highlight based on the parameter value:
<g:if test="${activeView == 'products'}">
<li class="menuItem active">Products</li><!-- not clickable if active -->
</g:if>
<g:else>
<li class="menuItem"><a href="products.gsp?activeView=products">Products</a></li>
</g:else>
I would also suggest having controllers as entry point rather than GSP.
<g:link controller="products" action="list" class="menuItem" params="[activeView:'products']">Book List</g:link>
a source to share
I think the sitemesh pageProperty is elegant in a non-invasive solution that keeps things completely in sight.
on a specific page:
<body active="home">
in your sitemesh layout template:
<g:if test="${pageProperty(name: 'body.active') == 'home'}">
<li class="active"><g:link uri="/">Home</g:link></li>
</g:if>
<g:else>
<li><g:link uri="/">Home</g:link></li>
</g:else>
a source to share