|
143 | 143 |
|
144 | 144 | 2. **Create a template for the navigation.** |
145 | 145 |
|
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. |
147 | 147 |
|
148 | 148 | 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. |
149 | 149 |
|
|
177 | 177 | </ul> |
178 | 178 | ``` |
179 | 179 |
|
| 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 | +
|
180 | 208 | 1. **Integrate navigation in templates.**: |
181 | 209 |
|
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. |
183 | 211 |
|
184 | 212 | For example: |
185 | 213 |
|
|
194 | 222 | {% endblock navigation %} |
195 | 223 | ``` |
196 | 224 |
|
| 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 | +
|
197 | 236 | The template tag can either take a string representing the import path to your navigation definition or an instance of your navigation class: |
198 | 237 |
|
199 | 238 | ```python |
|
217 | 256 | {% endblock navigation %} |
218 | 257 | ``` |
219 | 258 |
|
| 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 | +
|
220 | 270 | 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. |
221 | 271 |
|
222 | 272 | ```htmldjango |
|
228 | 278 | </footer> |
229 | 279 | ``` |
230 | 280 |
|
| 281 | + ```html |
| 282 | + <!-- base.html.j2 --> |
| 283 | +
|
| 284 | + <footer> |
| 285 | + {{ django_simple_nav("path.to.MainNav", "footer_nav.html.j2") }} |
| 286 | + </footer> |
| 287 | + ``` |
| 288 | +
|
231 | 289 | 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. |
232 | 290 | <!-- usage-end --> |
233 | 291 |
|
|
0 commit comments