變數
變數標籤會建立新的 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
的新數字變數。在後續呼叫時,它會將其值增加 1 並輸出新值。
輸入
{% 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
的新數字變數。在後續呼叫時,它會將其值減少 1 並輸出新值。
輸入
{% decrement variable %}
{% decrement variable %}
{% decrement variable %}
輸出
-1
-2
-3
與 increment 類似,使用 decrement
宣告的變數與使用 assign
或 capture
建立的變數獨立。