Template Tags and Filters¶
context_tags
module¶
You need to add missing.templatetags.context_tags
to your builtins
option
for the DjangoTemplates
backend to be able to use these tags immediately after
extends
tag.
A special
block
tag which does not render anything but can be used to modify a template context.The tag is rendered first thus modifying context before other blocks are rendered. A tag in an extending template is rendered after parent tags, allowing you to override template context in child templates.
The tag has to be the first tag, immediately after the
extends
tag. You have to define a empty context block tag at the very start of your base template.Example usage, in your base template:
{% contextblock %}{% endcontextblock %}<html> <body> <head> <title>{{ title }}</title> </head> <body> <h1>{{ title }}</h1> <p><a href="{{ homepage }}">{{ title }}</a></p> </body> </body> </html>
In your extending template:
{% extends "base.html" %} {% contextblock %} {% load future i18n %} {% setcontext as title %}{% blocktrans %}{{ username }}'s blog{% endblocktrans %}{% endsetcontext %} {% url "homepage" as homepage %} {% endcontextblock %}
Sets (updates) current template context with the rendered output of the block inside tags.
This is useful when some template tag does not support storing its output in the context itself or we need some complex content (like language, user or URL dependent content) multiple times.
Example usage:
{% setcontext as varname %} {% complextag %} {% endsetcontext %}
forloop_tags
module¶
Use {% load forloop_tags %}
in your template to load this module.
Maps loop variables of
for
tag to CSS classes string.Takes names of first, last, odd, even CSS classes, respectively
Example usage:
{% css_classes "first" "last" "odd" "even" %}
html_tags
module¶
Use {% load html_tags %}
in your template to load this module.
Filter which converts to a string suitable for use as an anchor id on a HTML element.
This is useful when you want anchor id on a heading to match heading content, which can be an arbitrary string.
Example usage:
<h1 id="{{ _("My Blog")|anchorify }}">{% trans "My Blog" %}</h1>
The result would be:
<h1 id="my-blog">My Blog</h1>
Renders heading with unique anchor id using a template.
Tag assures that each anchor id is unique inside the whole rendered template where it is used. Of course, only for headings created with the tag.
Heading level will be adjusted based on base heading level set by
set_base_heading_level()
orbase_heading_level
context variable. This is useful if you have some static main<h1>
with a site name and you want other headings to have a higher level automatically, but you want to reuse the same template you use can independently, without site name heading. Or if you include same template which uses this tag at various places where different heading level is needed based on the existing heading nesting. By default base heading level is 0, so no adjustment will be made, so example below will make a<h1>
heading.Optionally you can pass CSS classes string which will be passed through to the heading template. It uses
heading.html
ormissing/heading.html
template.Example usage:
{% heading 1 _("My Blog") %}
The result would be:
<h1 id="my-blog" class="heading ">My Blog</h1>
Set base heading level to a given numeric level to adjust heading levels of headings created by the
heading()
tag.You can also set base heading level by setting
base_heading_level
context variable. For example, by using built-inwith
tag.If you set
top_level
toTrue
, base heading level will be set at the top context level for the whole template. This is useful if you want to set base heading level for the whole template, but you are using the tag somewhere nested in blocks and includes. It will set base heading level only at the top context level so if you set heading level explicitly at some other context levels as well they will still take precedence.Example usage:
{% set_base_heading_level 1 %} {% heading 1 _("My Blog") %}
The result would be:
<h2 id="my-blog" class="heading ">My Blog</h2>
lang_tags
module¶
Use {% load lang_tags %}
in your template to load this module.
Translates given string to the specified language.
This is useful for text you need in some other language than the current language. For example, for links inviting users to switch to their language.
Example usage:
{% translate "Do you understand this?" "de" %}
list_tags
module¶
Use {% load list_tags %}
in your template to load this module.
Divides input list into the given number of sublists.
Last sublist can be shorter if input list length is not a multiplier of the given number of sublists.
Example usage:
<tr> {% for column in objects|divide_list:"2" %} <td><ul> {% for obj in column %} <li>{{ obj }}</li> {% endfor %} </ul></td> {% endfor %} </tr>
Splits input list into sublists of the given length.
Last sublist can be shorter if input list length is not a multiplier of the given length.
Example usage:
{% for group in objects|split_list:"4" %} <tr> {% for obj in group %} <td>{{ obj }}</td> {% endfor %} </tr> {% endfor %}
string_tags
module¶
Use {% load string_tags %}
in your template to load this module.
Returns the number of non-overlapping occurrences of an argument substring in the given string.
Ensures that string ends with dot if it does not already end with some punctuation.
Returns True if the given string starts with an argument prefix, otherwise returns False.
url_tags
module¶
Use {% load url_tags %}
in your template to load this module.
Returns
class_name
(defaultactive
) if any of givenurls
are real prefixes of the current request path.Useful when you want to highlight links to the current section of the site. For example, in menu entries.
Example usage:
{% active_url "/test/" %}
Builds an absolute (full) URL from the given location and the variables available in the request.
If no location is specified, the absolute (full) URL is built on
django.http.HttpRequest.get_full_path()
.It is a wrapper around
django.http.HttpRequest.build_absolute_uri()
. It requiresrequest
to be available in the template context (for example, by usingdjango.core.context_processors.request
context processor).Example usage:
{% url "view_name" as the_url %} {% fullurl the_url %}
Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens.
It is similar to built-in
slugify
but it also handles special characters in variety of languages so that they are not simply removed but properly transliterated/downcoded.
Creates URI template in a similar way to how
url
tags work but leaving parts of a URI to be filled in by a client. See RFC 6570 for more information.Names of parts are taken from named groups in URL regex pattern used for the view, or as a part’s sequence number (zero-based) for unnamed groups. You can pre-fill some parts by specifying them as additional arguments to the tag.
Warning
Tag cannot check if pre-fill values specified will really match back the URL regex pattern, so make sure yourself that they do.
Example usage:
{% with variable="42" %} {% urltemplate "view_name" arg1="value" arg2=variable %} {% endwith %}
If URL pattern would be defined like:
url(r'^some/view/(?P<arg1>.*)/(?P<arg2>.*)/(?P<param>.*)/$', some_view, name='view_name'),
The result would be:
/some/view/value/42/{param}/