Liquid Tag:条件-Conditional

条件-Conditional

  • if
  • else
  • case
  • unless

if

1.如果条件为true,则执行逻辑表达式

{% if condition %}
  expression
{% endif %}

说明:

  • condition:要判断的条件。
  • expression:如果满足条件则执行的表达式。

举例:

// 1.数据结构
{
  "product": {
    "compare_at_price": "10.00",
    "price": "0.00"
  }
}

// 2.代码执行
{% if product.compare_at_price > product.price %}
  This product is on sale!
{% endif %}

// 3.输出:
This product is on sale!

2.elsif

  • 注意:是elsif✅,而不是elseif ❌️

您可以使用elsif标签来检查多个条件。

// 1.数据结构
{
  "product": {
    "type": "Health"
  }
}

// 2.代码执行
{% if product.type == 'Love' %}
  This is a love potion!
{% elsif product.type == 'Health' %}
  This is a health potion!
{% endif %}

// 3.输出:
This is a health potion!

else

允许您指定在不满足其他条件时执行的默认表达式。您可以将该else标签与以下标签一起使用

  • case
  • if
  • unless

语法:

{% else %}
  expression

使用

// 1.数据
{
  "product": {
    "available": true
  }
}

// 2.代码
{% if product.available %}
  This product is available!
{% else %}
  This product is sold out!
{% endif %}

// 3.输出
This product is available!

case

根据特定变量的值呈现特定的表达式。

1.单个值判断

语法:

{% case variable %}
  {% when first_value %}
    first_expression
  {% when second_value %}
    second_expression
  {% else %}
    third_expression
{% endcase %}

说明:

  • variable:您想要作为案例陈述依据的变量的名称。
  • first_value:检查的第1个值,检查 variablefirst_value 这2个变量是否相等,如果相等,则执行:first_expression
  • second_value:检查的第2个值,检查 variablesecond_value 这2个变量是否相等,如果相等,则执行:second_expression
  • third_expression: 如果 variable 不等于 first_value && variable 不等于 second_value ,那么将执行 else部分,则执行:third_expression

举例:

// 1.数据
{
  "product": {
    "type": "Love"
  }
}


// 2.代码
{% case product.type %}
  {% when 'Health' %}
    This is a health potion.
  {% when 'Love' %}
    This is a love potion.
  {% else %}
    This is a potion.
{% endcase %}


// 3.输出
This is a love potion.

2.多个值

语法:

{% case variable %}
  {% when first_value or second_value or third_value %}
    first_expression
  {% when fourth_value, fifth_value, sixth_value %}
    second_expression
  {% else %}
    third_expression
{% endcase %}

说明:

  • 一个when标签可以接受多个值。
  • 当提供多个值时,只要变量与标签内的任意值匹配,就会返回相应的表达式
  • 两种方式:1.以逗号分隔的列表形式提供这些值,2.或使用or运算符进行分隔。

举例:

// 1.数据
{
  "product": {
    "type": "Strength"
  }
}


// 2.代码
{% case product.type %}
  {% when 'Love' or 'Luck' %}
    This is a love or luck potion.
  {% when 'Strength','Health' %}
    This is a strength or health potion.
  {% else %}
    This is a potion.
{% endcase %}


// 3.输出
This is a strength or health potion.

unless

当条件为false,否则执行里面的逻辑

  • 通俗的说法,就是当条件值为false,则执行里面的代码。当条件值为true,则不执行里面的代码
  • 因此unless和if是相反的,if是当条件为true则执行逻辑,unless是当条件为false则执行逻辑

语法:

{% unless condition %}
  expression
{% endunless %}

说明:

  • condition:条件
  • expression:当条件为false,则执行的逻辑

举例:

// 1.数据
{
  "product": {
    "has_only_default_variant": false
  }
}


// 2.代码
{% unless product.has_only_default_variant %}
  Variant selection functionality
{% endunless %}


// 3.输出
Variant selection functionality
Copyright © fecify.com 2025 all right reserved,powered by Gitbook该文件修订时间: 2025-09-05 17:04:10

results matching ""

    No results matching ""