1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
``{% nav %}`` tag
=================
.. image:: https://badge.fury.io/py/django-navtag.svg
:target: https://badge.fury.io/py/django-navtag
.. image:: https://codecov.io/gh/SmileyChris/django-navtag/branch/master/graph/badge.svg
:target: https://codecov.io/gh/SmileyChris/django-navtag
A simple Django template tag to handle navigation item selection.
.. contents::
:local:
:backlinks: none
Installation
------------
Install the package using pip:
.. code:: bash
pip install django-navtag
Usage
-----
Add the app to your ``INSTALLED_APPS`` setting:
.. code:: python
INSTALLED_APPS = (
# ...
'django_navtag',
)
Give your base template a navigation block something like this:
.. code:: jinja
{% 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:
.. code:: jinja
{% extends "base.html" %}
{% block nav %}
{% nav "home" %}
{{ block.super }}
{% endblock %}
.. note::
This works for multiple levels of template inheritance, due to the fact
that only the first ``{% nav %}`` call found will change the ``nav``
context variable.
Hierarchical navigation
-----------------------
To create a sub-menu you can check against, simply dot-separate the item:
.. code:: jinja
{% 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] %}``:
.. code:: jinja
{% 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:
.. code:: jinja
{% 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:
.. code:: jinja
<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** ``==``:
.. code:: jinja
{% nav "products.phones" %}
{% if nav == "products.phones" %}
{# True - exact match #}
{% endif %}
{% if nav == "products" %}
{# False - not exact #}
{% endif %}
**Special patterns with** ``!``:
.. code:: jinja
{% 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``:
.. code:: jinja
{% 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:
.. code:: jinja
{% 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:
.. code:: jinja
{% nav "products.electronics.phones" %}
{% for component in nav %}
{{ component }}
{# Outputs: products, electronics, phones #}
{% endfor %}
This also works with sub-navigation:
.. code:: jinja
{% nav "products.electronics.phones" %}
{% for component in nav.products %}
{{ component }}
{# Outputs: electronics, phones #}
{% endfor %}
The ``{% navlink %}`` tag
-------------------------
The ``{% navlink %}`` tag provides a convenient way to create navigation links that automatically change based on the active navigation state. It works as a block tag that renders different HTML elements depending on whether the navigation item is active.
Basic usage:
.. code:: jinja
{% load navtag %}
{% nav text 'active' %}
{% nav "products" %}
<ul class="nav">
{% navlink 'home' 'home_url' %}Home{% endnavlink %}
{% navlink 'products' 'product_list' %}Products{% endnavlink %}
{% navlink 'contact' 'contact_url' %}Contact{% endnavlink %}
</ul>
The tag will render:
- ``<a href="..." class="active">...</a>`` - when the nav item is active
- ``<a href="...">...</a>`` - when the nav item is a parent of the active item
- ``<span>...</span>`` - when the nav item is not active
The second parameter uses Django's built-in ``{% url %}`` tag syntax, so you can pass URL names with arguments:
.. code:: jinja
{% navlink 'product' 'product_detail' product_id=product.id %}
{{ product.name }}
{% endnavlink %}
Custom attributes
~~~~~~~~~~~~~~~~~
You can customize the attribute added to active links using ``{% nav text %}`` with an attribute format:
.. code:: jinja
{% nav text ' aria-selected="true"' %}
{% nav "home" %}
{% navlink 'home' 'home_url' %}Home{% endnavlink %}
{# Renders: <a href="/" aria-selected="true">Home</a> #}
Special matching patterns
~~~~~~~~~~~~~~~~~~~~~~~~~
The ``{% navlink %}`` tag supports special patterns for more precise matching:
**Children-only pattern** (``item!``):
.. code:: jinja
{% nav "courses.special" %}
{% navlink 'courses' 'course_list' %}All Courses{% endnavlink %}
{# Renders as link with class="active" #}
{% navlink 'courses!' 'course_detail' %}Course Details{% endnavlink %}
{# Renders as link with class="active" - only when nav is a child of courses #}
When ``courses`` is active (not a child), the first link is active but the second becomes a ``<span>``.
**Exclusion pattern** (``item!exclude``):
.. code:: jinja
{% nav "courses.special" %}
{% navlink 'courses!list' 'course_detail' %}Course (not list){% endnavlink %}
{# Renders as link - active for any child except 'list' #}
{% navlink 'courses!special' 'course_detail' %}Course (not special){% endnavlink %}
{# Renders as span - 'special' is excluded #}
You can also use these patterns with ``{% if %}`` statements:
.. code:: jinja
{% if nav == "courses!" %}
{# True - matches any child of courses #}
{% endif %}
Alternate nav context
~~~~~~~~~~~~~~~~~~~~~
To use a different navigation context variable, prefix the nav item with the variable name:
.. code:: jinja
{% nav "products" for mainnav %}
{% nav "settings" for sidenav %}
{% navlink 'mainnav:products' 'product_list' %}Products{% endnavlink %}
{% navlink 'sidenav:settings' 'user_settings' %}Settings{% endnavlink %}
|