Skip to content

Commit 8321c4d

Browse files
committed
Add documentation for Jinja
1 parent 798982c commit 8321c4d

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

README.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143

144144
2. **Create a template for the navigation.**
145145

146-
Create a template to render the navigation structure. This is just a standard Django template so you can use any Django template features you like.
146+
Create a template to render the navigation structure. This is a standard Django or Jinja 2 template so you can use any template features you like.
147147

148148
The template will be passed an `items` variable in the context representing the structure of the navigation, containing the `NavItem` and `NavGroup` objects defined in your navigation.
149149

@@ -177,9 +177,37 @@
177177
</ul>
178178
```
179179
180+
The same template in Jinja would be written as follows:
181+
182+
```html
183+
<!-- main_nav.html.j2 -->
184+
<ul>
185+
{% for item in items %}
186+
<li>
187+
<a href="{{ item.url }}"{% if item.active %} class="active"{% endif %}{% if item.baz %} data-baz="{{ item.baz }}"{% endif %}>
188+
{{ item.title }}
189+
</a>
190+
{% if item['items'] %}
191+
<ul>
192+
{% for subitem in item['items'] %}
193+
<li>
194+
<a href="{{ subitem.url }}"{% if subitem.active %} class="active"{% endif %}{% if item.foo %} data-foo="{{ item.foo }}"{% endif %}>
195+
{{ subitem.title }}
196+
</a>
197+
</li>
198+
{% endfor %}
199+
</ul>
200+
{% endif %}
201+
</li>
202+
{% endfor %}
203+
</ul>
204+
```
205+
206+
Note that unlike in Django templates we need to index the `items` field as a string in Jinja.
207+
180208
1. **Integrate navigation in templates.**:
181209
182-
Use the `django_simple_nav` template tag in your Django templates where you want to display the navigation.
210+
Use the `django_simple_nav` template tag in your Django templates (the `django_simple_nav` function in Jinja) where you want to display the navigation.
183211
184212
For example:
185213
@@ -194,6 +222,17 @@
194222
{% endblock navigation %}
195223
```
196224
225+
For Jinja:
226+
227+
```html
228+
<!-- base.html.j2 -->
229+
{% block navigation %}
230+
<nav>
231+
{{ django_simple_nav("path.to.MainNav") }}
232+
</nav>
233+
{% endblock navigation %}
234+
```
235+
197236
The template tag can either take a string representing the import path to your navigation definition or an instance of your navigation class:
198237
199238
```python
@@ -217,6 +256,17 @@
217256
{% endblock navigation %}
218257
```
219258
259+
```html
260+
<!-- example_app/example_template.html.j2 -->
261+
{% extends "base.html" %}
262+
263+
{% block navigation %}
264+
<nav>
265+
{{ django_simple_nav(nav) }}
266+
</nav>
267+
{% endblock navigation %}
268+
```
269+
220270
Additionally, the template tag can take a second argument to specify the template to use for rendering the navigation. This is useful if you want to use the same navigation structure in multiple places but render it differently.
221271
222272
```htmldjango
@@ -228,6 +278,14 @@
228278
</footer>
229279
```
230280
281+
```html
282+
<!-- base.html.j2 -->
283+
284+
<footer>
285+
{{ django_simple_nav("path.to.MainNav", "footer_nav.html.j2") }}
286+
</footer>
287+
```
288+
231289
After configuring your navigation, you can use it across your Django project by calling the `django_simple_nav` template tag in your templates. This tag dynamically renders navigation based on your defined structure, ensuring a consistent and flexible navigation experience throughout your application.
232290
<!-- usage-end -->
233291

0 commit comments

Comments
 (0)