变量
变量标签创建新的 Liquid 变量。
assign
创建一个新的命名变量。
输入
{% assign my_variable = false %}
{% if my_variable != true %}
This statement is valid.
{% endif %}
输出
This statement is valid.
将值用引号 "
包裹起来,以将其保存为字符串变量。
输入
{% assign foo = "bar" %}
{{ foo }}
输出
bar
capture
捕获开始和结束标签内的字符串,并将其赋值给一个变量。使用 capture
创建的变量存储为字符串。
输入
{% capture my_variable %}I am being captured.{% endcapture %}
{{ my_variable }}
输出
I am being captured.
使用 capture
,你可以使用其他使用 assign
创建的变量创建复杂的字符串。
输入
{% assign favorite_food = "pizza" %}
{% assign age = 35 %}
{% capture about_me %}
I am {{ age }} and my favorite food is {{ favorite_food }}.
{% endcapture %}
{{ about_me }}
输出
I am 35 and my favourite food is pizza.
increment
创建一个新的数字变量并输出,初始值为 0
。在后续调用中,它将其值增加一并输出新值。
输入
{% increment my_counter %}
{% increment my_counter %}
{% increment my_counter %}
输出
0
1
2
使用 increment
创建的变量独立于使用 assign
或 capture
创建的变量。
在下面的示例中,使用 assign
创建了一个名为“var”的变量。然后对同名的变量多次使用 increment
标签。请注意,increment
标签不影响使用 assign
创建的“var”的值。
输入
{% assign var = 10 %}
{% increment var %}
{% increment var %}
{% increment var %}
{{ var }}
输出
0
1
2
10
decrement
创建一个新的数字变量并输出,初始值为 -1
。在后续调用中,它将其值减一并输出新值。
输入
{% decrement variable %}
{% decrement variable %}
{% decrement variable %}
输出
-1
-2
-3
与 increment 类似,使用 decrement
声明的变量独立于使用 assign
或 capture
创建的变量。