跳到主要内容

Tags

Liquid tags 用于定义告知 template 执行操作的逻辑。

使用方式

Tags 使用花括号百分号分隔符 {% %} 包裹。分隔符内的文本在渲染时不会产生可见输出。

在下面的示例中,if tag 定义了需要满足的条件。如果 product.available 返回 true,则显示价格;否则显示"售罄"提示。

{% if product.available %}
Price: $12.88
{% else %}
Sorry, this product is sold out.
{% endif %}
{
"product": {
"available": true
}
}
Price: $12.88

带参数的 tags

某些 tags 接受参数。有些参数是必填的,有些是可选的。例如,for tag 可以接受 limit 参数,在特定索引处退出循环。

{% assign numbers = '1,2,3,4,5' | split: ',' %}

{% for item in numbers limit: 2 %}
{{ item }}
{% endfor %}
1
2