django.template.Library.simple_tag()
Many template tags take a number of arguments – strings or template variables – and return a result after doing some processing based solely on the input arguments and some external information. For example, a current_time
tag might accept a format string and return the time as a string formatted accordingly.
To ease the creation of these types of tags, Django provides a helper function, simple_tag
. This function, which is a method of django.template.Library
, takes a function that accepts any number of arguments, wraps it in a render
function and the other necessary bits mentioned above and registers it with the template system.
Our current_time
function could thus be written like this:
import datetime from django import template register = template.Library() @register.simple_tag def current_time(format_string): return datetime.datetime.now().strftime(format_string)
A few things to note about the simple_tag
helper function:
- Checking for the required number of arguments, etc., has already been done by the time our function is called, so we don’t need to do that.
- The quotes around the argument (if any) have already been stripped away, so we just receive a plain string.
- If the argument was a template variable, our function is passed the current value of the variable, not the variable itself.
Unlike other tag utilities, simple_tag
passes its output through conditional_escape()
if the template context is in autoescape mode, to ensure correct HTML and protect you from XSS vulnerabilities.
If additional escaping is not desired, you will need to use mark_safe()
if you are absolutely sure that your code does not contain XSS vulnerabilities. For building small HTML snippets, use of format_html()
instead of mark_safe()
is strongly recommended.
Auto-escaping for simple_tag
as described in the previous two paragraphs was added.
If your template tag needs to access the current context, you can use the takes_context
argument when registering your tag:
@register.simple_tag(takes_context=True) def current_time(context, format_string): timezone = context['timezone'] return your_get_current_time_method(timezone, format_string)
Note that the first argument must be called context
.
For more information on how the takes_context
option works, see the section on inclusion tags.
If you need to rename your tag, you can provide a custom name for it:
register.simple_tag(lambda x: x - 1, name='minusone') @register.simple_tag(name='minustwo') def some_function(value): return value - 2
simple_tag
functions may accept any number of positional or keyword arguments. For example:
@register.simple_tag def my_tag(a, b, *args, **kwargs): warning = kwargs['warning'] profile = kwargs['profile'] ... return ...
Then in the template any number of arguments, separated by spaces, may be passed to the template tag. Like in Python, the values for keyword arguments are set using the equal sign (“=
”) and must be provided after the positional arguments. For example:
{% my_tag 123 "abcd" book.title warning=message|lower profile=user.profile %}
It’s possible to store the tag results in a template variable rather than directly outputting it. This is done by using the as
argument followed by the variable name. Doing so enables you to output the content yourself where you see fit:
{% get_current_time "%Y-%m-%d %I:%M %p" as the_time %} <p>The time is {{ the_time }}.</p>
Please login to continue.