Simple DRY Tabs using Django 1.3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
**Placed in templates/includes/tabs.html**

<ul class="tab-menu">
    <li class="{% if active_tab == 'tab1' %} active{% endif %}"><a href="#">Tab 1</a></li>
    <li class="{% if active_tab == 'tab2' %} active{% endif %}"><a href="#">Tab 2</a></li>
    <li class="{% if active_tab == 'tab3' %} active{% endif %}"><a href="#">Tab 3</a></li>
</ul>



**Placed in your page template**

{% include "includes/tabs.html" with active_tab='tab1' %}

More like this

  1. Clear session data on login and logout by jb0t 5 years, 2 months ago
  2. DRY menu Custom Template Tag by sergzach 1 year, 2 months ago
  3. Use django-admin.py instead of manage.py by whiteinge 5 years ago
  4. Auto-documenting Django Models with Sphinx by Voightkampff 1 year, 9 months ago
  5. TaggedManager and TaggedQuerySet with chainable tagged() methods implemented with django-tagging by fish2000 3 years, 3 months ago

Comments

megido_kz (on May 12, 2011):

nice

#

cferreir (on August 5, 2011):

Thanks for sharing. This was very helpful. Finally got my navigation working where it supports multiple/nested menu levels and shows which ones are active.

#

sergzach (on March 26, 2012):

Thank you! When I created a snippet #2722 I did not take into account the option with of inclusion tag.

I think your solution is better.

My decision have only one plus (your have several ones). In case of #2722 inheritance is possible. Sometimes it is more convenient and conceptual than inclusion.

#

sergzach (on March 26, 2012):

And one more plus in #2722. In your case if you want to mek whole fragment different (not only class attribute) you should write several times very similar code for each option. But I would like DRY menu.

#

sergzach (on March 27, 2012):

Now I use your snippet instead of mine. Thanks a lot!

#

ccarnell (on August 2, 2012):

This is such a clean solution; thank you so much for taking the time to share this!

#

jnns (on June 18, 2013):

Thanks for this, I didn't know about {% include with %}. This is great!

#

(Forgotten your password?)