================================== 内建模板标签和过滤器 ================================== 此文档介绍了 Django 内贱的模板标签和过滤器. 在此建议您使用 :doc:`自动化文档 `, 如果他是可用的, 那么他还包含了自定义的模板标签和过滤器. .. _ref-templates-builtins-tags: 内建标签参考 ---------------------- .. highlightlang:: html+django .. templatetag:: autoescape autoescape ^^^^^^^^^^ 控制当前使用的自动转义行为. 这个标签带有 ``on`` 或 ``off`` 参数, 决定了块内是否自动转义. 该块由 ``endautoescape`` 标签结束. 当自动转义是生效的, 所有变量的内容将被自动转义成HTML字面值后输出(在这之前,其他的过滤器均被执行). 这等效于在所有变量上应用了 :tfilter:`escape` 过滤器. 唯一的例外是已被标记为"安全"的转义, 如由代码生成的变量, 或者使用了 :tfilter:`safe` 或 :tfilter:`escape` 过滤器. 简单的应用:: {% autoescape on %} {{ body }} {% endautoescape %} .. templatetag:: block block ^^^^^ 定义一个块, 可以被子模板覆盖. 参见 :ref:`模板继承 `. .. templatetag:: comment comment ^^^^^^^ 注释掉 ``{% comment %}`` 和 ``{% endcomment %}`` 之间的所有内容. .. templatetag:: csrf_token csrf_token ^^^^^^^^^^ 在 Django 1.1.X 中, 这是一个未定义的标签他返回一个空字符串以便向后兼容. 在 Django 1.2 和更高的版本中, 它使用了CSRF保护, 具体描述在这个文档中 :doc:`Cross Site Request Forgeries `. .. templatetag:: cycle cycle ^^^^^ 在遇到此标签时周期性的显示给定的字符串或变量. 在循环中, 当每经过一次循环, 就周期性的显示给定的字符串中的一个:: {% for o in some_list %} ... {% endfor %} 你也可以使用变量. 例如, 如果你有这两个模板变量, ``rowvalue1`` 和 ``rowvalue2``, 你可以像这样在这两个变量之间循环的输出其中一个:: {% for o in some_list %} ... {% endfor %} 请注意, 可变参数 (上面的 ``rowvalue1`` 和 ``rowvalue2``) **不会** 被自动转义! 所以你应当确保变量是安全的, 或者使用强制转义, 像这样:: {% for o in some_list %} ... {% endfor %} 您可以混合使用变量和字符串:: {% for o in some_list %} ... {% endfor %} 在某些情况下, 你可能想要在循环之外使用一些周期性变化的变量. 为了做到这点, 只需要给 ``{% cycle %}`` 标签使用"as"定义一个名字, 像这样:: {% cycle 'row1' 'row2' as rowcolors %} 从这里开始, 你就可以在模板中使用这个变量了. 当你想显示这个周期性变量的下一个值的时候, 只需要插入你之前定义好的变量名就可以了. 所以, 像下面这个模板:: ... ... ... ... 将会输出:: ... ... ... ... 你可以在 ``{% cycle %}`` 标签中使用任意数量的参数, 使用空格作为分隔. 如果一个参数被单引号(``'``)或者双引号(``"``)包围, 那么他将被当做是一个字符串. 如果不带引号, 则被视为一个模板变量. 请注意, 在循环中包含的变量将不会被转义. 这是因为模板标签没有转义他们的内容. 任何HTML或JavaScript代码将被渲染为原始值. 这有可能导致安全问题. ``{% cycle %}`` 标签在较早的Django版本中使用的语法和现在有很大的差别. 为了向后兼容, 你不应该在新的项目中使用这种语法, 但是还是有人会使用, 像下面这样:: {% cycle row1,row2,row3 %} 在这种语法中, 每个参数都被当做是一个字符串, 没有办法使用变量作为参数. 逗号和空格也无法被作为参数. 我们提到任何新的项目, 你不应该使用这个语法吗? .. versionadded:: 1.3 默认情况下, 当您使用 ``as`` 关键字标记cycle标签, 在使用 ``{% cycle %}`` 声明时将输出周期中的第一个值. 如果你想使用一个嵌套的循环或模板中的值, 这可能是一个问题. 如果你想只声明周期, 但不输出第一个值, 你可以添加一个 ``silent`` 关键字作为标签中的最后一个关键字. 例如:: {% for obj in some_list %} {% cycle 'row1' 'row2' as rowcolors silent %} {% include "subtemplate.html " %} {% endfor %} 这将在 ```` 的 ``class`` 属性上循环的输出 ``row1`` 和 ``row2``; 子模板将会被 ``rowcolors`` 变量所标记的 ```` 包围. 如果没有使用 ``silent`` 关键字, ``row1`` 将会在 ```` 元素之前被输出. 当silent关键字被使用在一个周期变量上, silent关键字也被自动的使用在了后续的这个变量上. 在下面这个模板中, 第二个 ``{% cycle %}`` 将不会输出任何值:: {% cycle 'row1' 'row2' as rowcolors silent %} {% cycle rowcolors %} .. templatetag:: debug debug ^^^^^ 输出整体的调试信息, 包括当前上下文和导入的模块. .. templatetag:: extends extends ^^^^^^^ 标记此模板继承的父模板的标签. 这个标签有两种使用方式: * ``{% extends "base.html" %}`` (使用引号) Django将使用字面值 ``"base.html"`` 作为所继承的父模板的名字. * ``{% extends variable %}`` 使用变量 ``variable``. 如果变量是一个字符串, Django会使用这个字符串作为所继承的父模板的名字. 如果变量是一个 ``Template`` 对象, Django会使用这个对象作为父模板. 更多内容请参见 :ref:`template-inheritance` 文档. .. templatetag:: filter filter ^^^^^^ Filters 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 %} .. note:: The :tfilter:`escape` and :tfilter:`safe` filters are not acceptable arguments. Instead, use the :ttag:`autoescape` tag to manage autoescaping for blocks of template code. .. templatetag:: firstof firstof ^^^^^^^ Outputs the first variable passed that is not False. Does NOT auto-escape variable values. 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. Any HTML or Javascript code contained in the printed variable will be rendered as-is, which could potentially lead to security issues. If you need 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 %}``. 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 ^^^^^^^^^^^^^ The ``for`` tag can take an optional ``{% empty %}`` clause that will be displayed if the given array is empty or could not be found::