I want to highlight the current page in the navigation menu. Obviously, I need the menu to display a class such as βactiveβ when you are on your page. This is a classic problem and I have seen many solutions. My problem is that I hate them all and I think that not one of them is very DRY. For example:
@register.simple_tag def active(request, pattern): import re if re.search(pattern, request.path): return 'active' return '' ---- {% load tags %} <div id="navigation"> <a class="{% active request "^/about/" %}" href="/about/">About</a> <a class="{% active request "^/contact/" %}" href="/contact/">Contact</a> <a class="{% active request "^/services/" %}" href="/services/">Services</a> </div>
The tag accepts your current request and url expression and returns "active" if you are currently on this page. Alternatively, this can be done with named views, not with URLs, but the principle is the same.
My main problem is that my navigation will be called up 99% of my views, and yet, to get the current request variable, I still parse the RequestContext for the template like this:
def contact(request):
Why do I need to add this context_instance line to each of my views when, possibly, all but one need a request variable to get the current url / view in order to highlight the active link? This seems terribly wet, especially for a feature that should be in the vast majority of django sites. I want the request to be enabled by default and be able to suppress it. I cannot find a way to do this in the middleware, since I cannot intercept the template before displaying it after the view has returned it.
Any suggestions?
django django-templates django-views navigation
Jimmy Jun 27 '09 at 7:37 2009-06-27 07:37
source share