Skip to main content

A simple Django navigation template tag

Project description

https://badge.fury.io/py/django-navtag.svg https://codecov.io/gh/SmileyChris/django-navtag/branch/master/graph/badge.svg

A simple Django template tag to handle navigation item selection.

Installation

Install the package using pip:

pip install django-navtag

Usage

Add the app to your INSTALLED_APPS setting:

INSTALLED_APPS = (
    # ...
    'django_navtag',
)

Give your base template a navigation block something like this:

{% load navtag %}

{% block nav %}
{% nav text ' class="selected"' %}
<ul class="nav">
    <li{{ nav.home }}><a href="/">Home</a></li>
    <li{{ nav.about }}><a href="/about/">About</a></li>
</ul>
{% endblock %}

In your templates, extend the base and set the navigation location:

{% extends "base.html" %}

{% block nav %}
{% nav "home" %}
{{ block.super }}
{% endblock %}

Hierarchical navigation

To create a sub-menu you can check against, simply dot-separate the item:

{% nav "about_menu.info" %}

This will be pass for both {% if nav.about_menu %} and {% if nav.about_menu.info %}.

Using a different context variable

By default, this tag creates a nav context variable. To use an alternate context variable name, call {% nav [item] for [var_name] %}:

{% block nav %}
{% nav "home" for sidenav %}
{{ block.super }}
{% endblock %}

Setting the text output by the nav variable

As shown in the initial example, you can set the text return value of the nav context variable. Use the format {% nav text [content] %}. For example:

{% nav text "active" %}
<ul>
<li class="{{ nav.home }}">Home</li>
<li class="{{ nav.contact }}">Contact</li>
</ul>

Alternately, you can use boolean comparison of the context variable rather than text value:

<section{% if nav.home %} class="wide"{% endif %}>

If using a different context variable name, use the format {% nav text [content] for [var_name] %}.

Comparison operations

The nav object supports comparison operations for more flexible navigation handling:

Exact matching with ==:

{% nav "products.phones" %}

{% if nav == "products.phones" %}
    {# True - exact match #}
{% endif %}

{% if nav == "products" %}
    {# False - not exact #}
{% endif %}

Special patterns with !:

{% nav "products.electronics" %}

{% if nav == "products!" %}
    {# True - matches any child of products #}
{% endif %}

{% if nav == "products!clearance" %}
    {# True - matches children except 'clearance' #}
{% endif %}

Component checking with in:

{% nav "products.electronics.phones" %}

{% if "products" in nav %}
    {# True - component exists #}
{% endif %}

{% if "electronics" in nav %}
    {# True - component exists #}
{% endif %}

{% if "tablets" in nav %}
    {# False - component doesn't exist #}
{% endif %}

These operations also work with sub-navigation:

{% nav "products.electronics.phones" %}

{% if nav.products == "electronics.phones" %}
    {# True #}
{% endif %}

{% if "electronics" in nav.products %}
    {# True #}
{% endif %}

Iteration

The nav object supports iteration over its active path components:

{% nav "products.electronics.phones" %}

{% for component in nav %}
    {{ component }}
    {# Outputs: products, electronics, phones #}
{% endfor %}

This also works with sub-navigation:

{% nav "products.electronics.phones" %}

{% for component in nav.products %}
    {{ component }}
    {# Outputs: electronics, phones #}
{% endfor %}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_navtag-3.3.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_navtag-3.3.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file django_navtag-3.3.1.tar.gz.

File metadata

  • Download URL: django_navtag-3.3.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.2

File hashes

Hashes for django_navtag-3.3.1.tar.gz
Algorithm Hash digest
SHA256 33099889d5b7df29c6028901226df1e8631f31567df94355fcd472b6af891462
MD5 71b26f11e70cfc1e7d71e655073412b5
BLAKE2b-256 21e47b3e28d0502ceae70f60581b931eb47409e45b9874b5b0a736246c37e56b

See more details on using hashes here.

File details

Details for the file django_navtag-3.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_navtag-3.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 36a2901658f035f013cfa65b49e925f7dec346dafafaa84a3999b206e106bac4
MD5 34b82c3fae40be281a6b88058d00778f
BLAKE2b-256 3fc4b0247979cc1ce9732096ee04300c5b05d392eafe5d511b1c46d5f524ea47

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page