views:

22

answers:

3

I'm looking to put navigation in my GSP template, and I would like to set the active class on the navigation elements for each respective page. What's the best way to do this? I have several .gsp views merging with a single 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>
+1  A: 

This is usually done by passing a param, let's call it activeView. Then in your menu template you can check which menu item to highlight based on the param's 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 the point of entry, and not GSPs.

  <g:link controller="products" action="list" class="menuItem" params="[activeView:'products']">Book List</g:link>
armandino
+1  A: 

Another idea is to use the the Grails navigation plugin.

Stefan
+1 for having my name..../useful link.
Stefan Kendall
+1  A: 

I like armandino's suggestion, however you may have problems if you're accessing pages by other means than clicking the menus (eg through a bookmark or the first page after login).

This is another solution if you're using SiteMesh, however it is not isolated to the menu template and hence not as nice design-wise:

Grails active page navigation menu

wwwclaes
This is probably the simplest and quickest way to implement this. It doesn't scale well, but that's not an issue in my case.
Stefan Kendall