如何比较液体变量?

时间:2018-01-12 10:08:51

标签: shopify liquid

我有5个液体变量,它们都有一个数字。例如{{wa}}输出100.有没有办法比较这些值并输出最高数字?我在Liquid中试过这个无济于事:

{% if wa > bh and wa > ag and wa > jr and wa > jg %}<span>{{ wa }}</span>
  {% elsif bh > wa and bh > ag and bh > jr and bh > jg %}<span>{{ bh }}</span>
  {% elsif ag > wa and ag > bh and ag > jr and ag > jg %}<span>{{ ag }}</span>
  {% elsif jr > wa and jr > bh and jr > ag and jr > jg %}<span>{{ jr }}</span>
  {% elsif jg > wa and jg > bh and jg > ag and jg > jr %}<span>{{ jg }}</span>
{% endif %}

干杯!

1 个答案:

答案 0 :(得分:2)

您可以这样做:

{% assign numbers = '12,323,9,121,11,1,1116,12,3,1' %}
{% assign numbers_array = numbers | split: ',' %}

{% assign highest_number = numbers_array | first | plus: 0 %}

{% for number in numbers_array %}
    {% assign cur_number = number | plus: 0 %}

    {% if cur_number >= highest_number %}

        {% assign highest_number = number | plus: 0 %}
    {% endif %}
{% endfor %}

The Highest Number is: {{ highest_number }}

创建一个字符串,其中所有数字都以,分隔,并按,拆分,以便创建数组。

将第一个数字保存为最高数字并开始循环,将每个数字与highest_number中设置的当前数字进行比较。如果cur_number更高,我们会将该数字设置为highest_number

这几乎就是逻辑。