Inclusion Tags
Another common template tag is the type that displays some data by rendering another template. For example, Django’s admin interface uses custom template tags to display the buttons along the bottom of the “add/change” form pages. Those buttons always look the same, but the link targets change depending on the object being edited. They’re a perfect case for using a small template that is filled with details from the current object.
These sorts of tags are called inclusion tags. Writing inclusion tags is probably best demonstrated by example. Let’s write a tag that produces a list of books for a given Author object. We’ll use the tag like this:
The result will be something like this:
_______________
Pre-conditions:
custom tag creation and loading
polls/
models.py
templatetags/
__init__.py
poll_extras.py
views.py
And in your template you would use the following:
{% load poll_extras %}
_________________
First, we define the function that takes the argument and produces a dictionary of data for the result. Notice that we need to return only a dictionary, not anything more complex. This will be used as the context for the template fragment:
Next, we create the template used to render the tag’s output. Following our example, the template is very simple:
book_snippet.html
Finally, we create and register the inclusion tag by calling the inclusion_tag() method on a Library object.
Following our example, if the preceding template is in a file called book_snippet.html, we register the tag like this:
Python 2.4 decorator syntax works as well, so we could have written this, instead:
takes_context
Sometimes, your inclusion tags need access to values from the parent template’s context. To solve this, Django provides a takes_context option for inclusion tags. If you specify takes_context in creating an inclusion tag, the tag will have no required arguments, and the underlying Python function will have one argument: the template context as of when the tag was called.
For example, say you’re writing an inclusion tag that will always be used in a context that containshome_link and home_title variables that point back to the main page. Here’s what the Python function would look like:
(Note that the first parameter to the function must be called context.)
The template link.html might contain the following:
Then, anytime you want to use that custom tag, load its library and call it without any arguments, like so:
No comments:
Post a Comment