【发布时间】:2020-05-10 05:59:48
【问题描述】:
我正在为 Shopify 使用 Liquid 模板。我希望仅当月份恰好是 12 月时才显示一些元素。由于有多个元素需要这个,我想在文档顶部设置一个变量,以便稍后参考。这就是我的工作:
<!-- At the top of the page -->
{% assign month = 'now' | date: "%m" %}
{% if month == "12" %}
{% assign isDecember = true %}
{% else %}
{% assign isDecember = false %}
{% endif %}
<!-- Only show in December -->
{% if isDecember %}
Happy Holidays
{% endif %}
这可行(为了测试我将“12”更改为当前月份),但它非常难看。在大多数语言中,我会这样做:
{% assign isDecember = (month == "12") %}
Liquid 不接受括号,所以显然这行不通。没有括号它也不起作用。文档中有 using operators 和 assigning static values to variables 的示例,但没有关于将两者结合起来的内容。
我可以将| 过滤器的输出分配给一个变量,但似乎没有过滤器可以覆盖每个运算符(甚至是必要的“==”),所以这并不令人满意。
有没有办法将运算符的输出分配给 Liquid 中的变量?
【问题讨论】:
标签: operators variable-assignment liquid assign assignment-operator