Reputation: 3075
New to Django and using the template engine for the first time. My question is...
I have a navigation div in my base.html. Now in other languages I would use a include for nav element of the html. In Django I see we have something called a template tag. So should I use template tag to display presentation elements like nav or old school includes (which I believe Django also supports)?
Also to keep this a stackoverflow question, an example would be useful.
Thanks
Upvotes: 2
Views: 1061
Reputation: 13328
Template tags are really for applying special processing to achieve the output you want. Using a template tag gives you access to all the functionality of the python language. The django template language is much more simple.
I would suggest that you want to stick with include rather than write a template tag. It will be easier to read.
Remember that you should be producing your list of variables in the view (or somewhere outside the template / template tag).
def your_view(request):
#...
nav_items = Category.objects.filter(include_in_nav=True)
return render(request, 'your_page.html', {'nav_items': nav_items})
Then in your_page.html you're going to do -
{% include 'nav.html' %}
Then nav.html
will be something like -
<nav>
{% for item in nav_items %}
<a href="{% url cat_page item %}>{{ item.title }}</a>
{% endfor %}
</nav>
Have a look at the docs on templates and custom template tags.
Upvotes: 3