.. _ref-templates-builtins: ==================================== 組み込みタグ/フィルタリファレンス ==================================== :revision-up-to: 11321 (1.1) unfinished このドキュメントでは、 Django の組み込みテンプレートタグおよびフィルタにつ いて解説しています。 また、 :ref:`自動生成ドキュメント ` を使えば、インストールされている組み込みタグ とカスタムタグのドキュメントを読めるのでお勧めです。 .. _ref-templates-builtins-tags: 組み込みタグリファレンス ------------------------ .. highlightlang:: html+django .. templatetag:: autoescape autoescape ~~~~~~~~~~ .. versionadded:: 1.0 自動エスケープ機能を制御します。このタグは引数に ``on`` または ``off`` を取 り、ブロック内の自動エスケープの有効・無効を決定します。 自動エスケープがオンの場合、変数の値は全て、最終的な文字列出力になる直前に HTML エスケープされます (他のフィルタは先に適用されます)。この動作は、 変数に ``escape`` フィルタを手動で適用した場合と同じです。 例外として、変数をテンプレートに挿入するコードや、 ``safe``, ``escape`` と いったフィルタの適用によって、 "safe" マーク済みの変数はエスケープされませ ん。 .. templatetag:: block block ~~~~~ 子テンプレートでオーバライドできるブロックを定義します。 :ref:`テンプレートの継承 ` を参照してください。 .. templatetag:: comment comment ~~~~~~~ ``{% comment %}`` から ``{% endcomment %}`` までの内容を全て無視します。 .. templatetag:: cycle cycle ~~~~~ .. versionchanged:: 1.0 タグを処理するごとに、指定した文字列や変数を循環して返します。 ループの中では、ループごとに指定した文字列や変数を循環して返します:: {% 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 ~~~~~ 現在のコンテキストや import されたモジュールなどを含んだデバッグ情報ひと揃 いを出力します。 .. templatetag:: extends extends ~~~~~~~ このテンプレートが親テンプレートに対する拡張であることを指示します。 このタグには 2 種類の使い方があります: * ``{% extends "base.html" %}`` (引用符つき) のような場合、リテラル値 ``"base.html"`` を親テンプレートの名前として使います。 * ``{% extends variable %}`` のようにすると、 ``variable`` の値を親テンプ レートの名前として使います。 ``variable`` の値が文字列の場合、 Django はその文字列を親テンプレートの名前として使います。値が ``Template`` オ ブジェクトの場合、Django はそのオブジェクトを親テンプレートにします。 詳しくは :ref:`template-inheritance` を参照してください。 .. templatetag:: filter filter ~~~~~~ タグのコンテンツを変数フィルタ (variable filter) を使ってフィルタします。 フィルタはパイプでつないで連鎖でき、引数をもたせることができます。 使用例:: {% filter force_escape|lower %} This text will be HTML-escaped, and will appear in all lowercase. {% endfilter %} .. templatetag:: firstof firstof ~~~~~~~ タグに渡された変数のうち、False でない最初の変数の値を出力します。全ての変 数が False であった場合、何も出力しません。 使用例:: {% firstof var1 var2 var3 %} 上は、以下のテンプレートと等価です:: {% if var1 %} {{ var1 }} {% else %}{% if var2 %} {{ var2 }} {% else %}{% if var3 %} {{ var3 }} {% endif %}{% endif %}{% endif %} また、全ての変数が 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 ~~~ アレイの各要素に渡ってループします。例えば、アスリート (athlete) のリストを ``athlete_list`` で渡して表示するには:: ``{% for obj in list reversed %}`` のようにすると、リストに対して逆順のルー プを実行できます。 .. versionadded:: 1.0 リストのリストにわたってループ処理を行う場合、各サブリストをアンパックして、 個別に名前を割り当てられます。例えば、座標 (x, y) のリストが入った ``points`` というコンテキスト変数があり、各座標を出力したい場合には以下のよ うにします:: {% for x, y in points %} 座標 {{ x }},{{ y }} が登録されています。 {% endfor %} この方法は、辞書の各要素にアクセスしたい場合にも便利です。例えば、コンテキ スト変数 ``data`` に辞書が入っている場合。以下のようにすれば辞書内のキーと 値を表示できます:: {% for key, value in data.items %} {{ key }}: {{ value }} {% endfor %} for ループは、ループの各回ごとに使える変数を設定します: ========================== ================================================ 変数名 説明 ========================== ================================================ ``forloop.counter`` 現在のループ回数番号 (1 から数えたもの) ``forloop.counter0`` 現在のループ回数番号 (0 から数えたもの) ``forloop.revcounter`` 末尾から数えたループ回数番号 (1 から数えたもの) ``forloop.revcounter0`` 末尾から数えたループ回数番号 (0 から数えたもの) ``forloop.first`` 最初のループであれば True になります ``forloop.last`` 最後のループであれば True になります ``forloop.parentloop`` 入れ子のループの場合、一つ上のループを表します ========================== ================================================ 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::