Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django: same template tag but in multiple inherited html templates

I use template inheritance in django.

Currently have frame.html and book_detail.html

book_detail.html extends frame.html and I have books_tags.py

# In frame.html
{% load books_tags %}
{% book_list %} # using a tag from books_tags.py



# In books.html
{% extends frame.html %}
{% block content %}
   {% book_list %} # I want to use this from books_tags.py also
{% endblock %}

When I try to use book_list from the same books_tags.py, it gives me syntax error. Alternately, when I put {% load books_tags %} inside book_detail.html first, it again still gives me syntax error.

Invalid block tag: 'book_list', expected 'endblock' or 'endblock content'

Is there any way I can use same template tags in multiple inherited templates? Or do I need to create frame_tags.py and book_detail_tags.py with same def in it?

Since default template filters in django works in multiple inheritance, I'm guessing it should work, but don't know how to get by syntax error.

Thank you.

== UPDATE ==

Add template_tag inside {% block content %} in books.html again.

# In books.html
{% extends frame.html %}
{% block content %}
   **{% load books_tags %}**
   {% book_list %} # I want to use this from books_tags.py also
{% endblock %}
like image 338
David Avatar asked Feb 13 '11 23:02

David


1 Answers

You can use the same template tags in multiple templates, but you have to call {% load books_tags %} in each file, even if there is an inheritance relationship between them.

See https://docs.djangoproject.com/en/dev/topics/templates/#custom-libraries-and-template-inheritance

like image 186
Arnaud Avatar answered Nov 15 '22 08:11

Arnaud