cycle
Changed in Django 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 %}
<tr class="{% cycle 'row1' 'row2' %}">
...
</tr>
{% 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 %}
<tr class="{% cycle rowvalue1 rowvalue2 %}">
...
</tr>
{% endfor %}
Yes, you can mix variables and strings:
{% for o in some_list %}
<tr class="{% cycle 'row1' rowvalue2 'row3' %}">
...
</tr>
{% 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:
<tr class="{% cycle rowcolors %}">...</tr>
<tr class="{% cycle rowcolors %}">...</tr>
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?
分享到:
相关推荐
Built-In Building Blocks.dotx office2016 .
本资源包含 Unity 内置 Shader,即 built-in-shaders,版本:2022.2.7f1
unity3d built-in shader源代码
Functions - Spark SQL, Built-in Functions,强大的sql全部语法。
ServoCenter 4.1 SC-BASIC Built-in Sequencerpdf,ServoCenter 4.1伺服控制器内置SC-BASIC编译器Sequencer程序。
Oracle-Built-In-Packages
QnUCPU User's Manual (Communication via Built-in Ethernet Port)
Apple-Broadcom-Built-in-Bluetooth-6.0.6100.0.zip
The webMethods Integration Server Built-In Services Reference describes the built-in services provided with a standard installation of the webMethods Integration Server. Services are also installed ...
Sony IMX185 datasheet,其中包含线性datasheet和Built-In WDR的datasheet. 完整的文档
Oracle Built-in Packages原文,可以当手册使用,2010年出版的。
Unity 重内置 Built-in 到 可编程 URP 函数对应整理.docx
英文非扫描版,超级经典的书,有想自己牛逼的人有必要看
一种通信装备Built-In Test技术研究与实现.caj
A new static test of a DAC with a built-in structure
Android按钮内带进度条Android Buttons With Built-in progress 可以用于登陆按钮等地方
SkinControls Button, CheckBox and RadioButton controls with built-in styles<br><br>这是一个按钮的C#源代码,<br>来自www.codeproject.com的源代码...
腾龙镜头4x_4k 12.5 Built-in Zoom Lens Specification是腾龙镜头、4K镜头、小倍镜头规格书。
An awesome and customizable search dialog with built-in search options..zip,快速可定制的搜索对话框