I have a template block tag {% ifactive %} that lets you highlight the
currently active page using similar syntax as the {% url %} tag (i.e.,
you can specify urlpattern names and arguments in the same way), e.g.:

<a {% ifactive request page1 %}class='active'{% endifactive %}
href='{% url
page1 %}'>Page 1</a>
<a {% ifactive request page2 %}class='active'{% endifactive %}
href='{% url
page2 %}'>Page 2</a>
...
<a {% ifactive request pageN %}class='active'{% endifactive %}
href='{% url
pageN %}'>Page N</a>

The code for this is at http://demo.apture.com/demo/ifactive.py .

I admit that the syntax {% ifactive request page2 %}...{% endifactive
%} is somewhat verbose, but the link above also explains how to write
specific template tags that are customized for your own templates,
e.g.:

<a {% activeif page2 %} href='{% url page2 %}'>Page 2</a>

I like this method because it determines whether a page is active by
checking if the same view is called with the same arguments, instead
of just comparing URLs. So it can handle cases where different URLs
map to the same view. It also doesn't require modifying any of the
child templates, like some of the solutions described above.

-Jesse

On Oct 17, 6:02 pm, Nathaniel Whiteinge <[EMAIL PROTECTED]> wrote:
> A slight variation that doesn't require repeating the whole navigation
> div in each base_SECTION.html template:
>
> # base.html
> {% block content %}...{% endblock %}
> {% block navigation %}
> <ul>
>     <li{% block active_section_1 %}{% endblock %}><a href="#">Section
> 1</a></li>
>     <li{% block active_section_2 %}{% endblock %}><a href="#">Section
> 2</a></li>
> </ul>
> {% endblock %}
>
> # base_section1.html
> {% extends "base.html" %}
> {% block active_section_1 %} class="active"{% endblock %}
>
> # base_section2.html
> {% extends "base.html" %}
> {% block active_section_2 %} class="active"{% endblock %}
>
> # section1_specific_page.html
> {% extends "base_section1.html" %}
> {% block content %}...{% endblock %}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to