liquid真值假值

122 阅读1分钟

All data types must return either true or false. Those which return true by default are called truthy. Those that return false by default are called falsy.

liquid数据真假

liquid数据真假以及判空

Because nil and false are the only falsy values, you need to be careful how you check values in Liquid. A value might not be in the format you expect, but still be truthy.

For example, empty strings are truthy, so you need to check whether they’re empty with blank. EmptyDrop objects are also truthy, so you need to check whether the object you’re referencing is empty.

因为只有false和nil是假值,而对于空字符串和空对象的判断就不能简单跟假值对比

  • 空字符串的判断要用blank
  • 空对象的判断要用empty
// 字符串判空
{% if settings.featured_potions_title != blank -%}
{{ settings.featured_potions_title }}
{%- else -%}
No value for this setting has been selected.
{%- endif %}
// 对象判空
{% unless pages.recipes == empty -%}
{{ pages.recipes.content }}
{%- else -%}
No page with this handle exists.
{%- endunless %}