.. _ref-templates-builtins: ================================== Built-in template tags and filters ================================== This document describes Django's built-in template tags and filters. It is recommended that you use the :ref:`automatic documentation `, if available, as this will also include documentation for any custom tags or filters installed. .. _ref-templates-builtins-tags: Built-in tag reference ---------------------- .. highlightlang:: html+django .. templatetag:: autoescape autoescape ~~~~~~~~~~ .. versionadded:: 1.0 Control the current auto-escaping behavior. This tag takes either ``on`` or ``off`` as an argument and that determines whether auto-escaping is in effect inside the block. When auto-escaping is in effect, all variable content has HTML escaping applied to it before placing the result into the output (but after any filters have been applied). This is equivalent to manually applying the ``escape`` filter to each variable. The only exceptions are variables that are already marked as "safe" from escaping, either by the code that populated the variable, or because it has had the ``safe`` or ``escape`` filters applied. .. templatetag:: block block ~~~~~ Define a block that can be overridden by child templates. See :ref:`Template inheritance ` for more information. .. templatetag:: comment comment ~~~~~~~ Ignore everything between ``{% comment %}`` and ``{% endcomment %}`` .. templatetag:: cycle cycle ~~~~~ .. versionchanged:: 1.0 Cycle among the given strings or variables each time this tag is encountered. Within a loop, cycles among the given strings each time through the loop:: {% for o in some_list %} ... {% endfor %} You can use variables, too. For example, if you have two template variables, ``rowvalue1`` and ``rowvalue2``, you can cycle between their values like this:: {% for o in some_list %} ... {% endfor %} Yes, you can mix variables and strings:: {% for o in some_list %} ... {% endfor %} In some cases you might want to refer to the next value of a cycle from outside of a loop. To do this, just give the ``{% cycle %}`` tag a name, using "as", like this:: {% cycle 'row1' 'row2' as rowcolors %} From then on, you can insert the current value of the cycle wherever you'd like in your template:: ... ... You can use any number of values in a ``{% cycle %}`` tag, separated by spaces. Values enclosed in single (``'``) or double quotes (``"``) are treated as string literals, while values without quotes are treated as template variables. Note that the variables included in the cycle will not be escaped. This is because template tags do not escape their content. If you want to escape the variables in the cycle, you must do so explicitly:: {% filter force_escape %} {% cycle var1 var2 var3 %} {% endfilter %} For backwards compatibility, the ``{% cycle %}`` tag supports the much inferior old syntax from previous Django versions. You shouldn't use this in any new projects, but for the sake of the people who are still using it, here's what it looks like:: {% cycle row1,row2,row3 %} In this syntax, each value gets interpreted as a literal string, and there's no way to specify variable values. Or literal commas. Or spaces. Did we mention you shouldn't use this syntax in any new projects? .. templatetag:: debug debug ~~~~~ Output a whole load of debugging information, including the current context and imported modules. .. templatetag:: extends extends ~~~~~~~ Signal that this template extends a parent template. This tag can be used in two ways: * ``{% extends "base.html" %}`` (with quotes) uses the literal value ``"base.html"`` as the name of the parent template to extend. * ``{% extends variable %}`` uses the value of ``variable``. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a ``Template`` object, Django will use that object as the parent template. See :ref:`template-inheritance` for more information. .. templatetag:: filter filter ~~~~~~ Filter the contents of the variable through variable filters. Filters can also be piped through each other, and they can have arguments -- just like in variable syntax. Sample usage:: {% filter force_escape|lower %} This text will be HTML-escaped, and will appear in all lowercase. {% endfilter %} .. templatetag:: firstof firstof ~~~~~~~ Outputs the first variable passed that is not False, without escaping. Outputs nothing if all the passed variables are False. Sample usage:: {% firstof var1 var2 var3 %} This is equivalent to:: {% if var1 %} {{ var1|safe }} {% else %}{% if var2 %} {{ var2|safe }} {% else %}{% if var3 %} {{ var3|safe }} {% endif %}{% endif %}{% endif %} You can also use a literal string as a fallback value in case all passed variables are False:: {% firstof var1 var2 var3 "fallback value" %} Note that the variables included in the firstof tag will not be escaped. This is because template tags do not escape their content. If you want to escape the variables in the firstof tag, you must do so explicitly:: {% filter force_escape %} {% firstof var1 var2 var3 "fallback value" %} {% endfilter %} .. templatetag:: for for ~~~ Loop over each item in an array. For example, to display a list of athletes provided in ``athlete_list``:: You can loop over a list in reverse by using ``{% for obj in list reversed %}``. .. versionadded:: 1.0 If you need to loop over a list of lists, you can unpack the values in each sub-list into individual variables. For example, if your context contains a list of (x,y) coordinates called ``points``, you could use the following to output the list of points:: {% for x, y in points %} There is a point at {{ x }},{{ y }} {% endfor %} This can also be useful if you need to access the items in a dictionary. For example, if your context contained a dictionary ``data``, the following would display the keys and values of the dictionary:: {% for key, value in data.items %} {{ key }}: {{ value }} {% endfor %} The for loop sets a number of variables available within the loop: ========================== ================================================ Variable Description ========================== ================================================ ``forloop.counter`` The current iteration of the loop (1-indexed) ``forloop.counter0`` The current iteration of the loop (0-indexed) ``forloop.revcounter`` The number of iterations from the end of the loop (1-indexed) ``forloop.revcounter0`` The number of iterations from the end of the loop (0-indexed) ``forloop.first`` True if this is the first time through the loop ``forloop.last`` True if this is the last time through the loop ``forloop.parentloop`` For nested loops, this is the loop "above" the current one ========================== ================================================ for ... empty ^^^^^^^^^^^^^ .. versionadded:: 1.1 The ``for`` tag can take an optional ``{% empty %}`` clause that will be displayed if the given array is empty or could not be found::