如何输出与之相关的最高分和名称 - 液体逻辑

时间:2018-01-12 16:03:30

标签: shopify liquid

我需要输出highest_score和与之关联的名称。代码输出highest_score,但它与正确的名称无关。我想它在for循环中需要一个for,但我不太确定如何构造它。

这是我的代码:

{%- capture list_of_scores -%}
{{wa}}|Wine Advocate,{{bh}}|Burghound,{{ag}}|Vinous,{{jr}}|Jancis Robinson,{{jg}}|John Gilman
{%- endcapture -%}
{%- capture list_of_scores_num -%}{{wa}},{{bh}},{{ag}},{{jr}},{{jg}}{%- endcapture -%}

{% assign scores_array = list_of_scores | split: ',' %}
{% assign scores = list_of_scores_num | split: ',' %}
{% assign highest_score = scores | first | plus: 0 %}


{% for score_val in scores %}
{% assign cur_score = score_val | plus: 0 %}
{% if cur_score >= highest_score %}
{% assign highest_score = score_val | plus: 0 %}
{% endif %}
{% endfor %}

{% for score_and_name in scores_array %}
{% assign split_score_and_name = score_and_name | split: '|' %}
{% assign score = split_score_and_name[0] %}
{% assign score = highest_score %}
{% assign name = split_score_and_name[1] %}
{% endfor %}

<span>{{ highest_score }}</span>
<h5>{{ name }}</h5>

由于

1 个答案:

答案 0 :(得分:1)

你可以做这样的事情

{% assign wa = 12 %}
{% assign bh = 16 %}
{% assign ag = 26 %}
{% assign jr = 6 %}
{% assign jg = 11 %}

{%- capture list_of_scores -%}
    {{wa}}|Wine Advocate,
    {{bh}}|Burghound,
    {{ag}}|Vinous,
    {{jr}}|Jancis Robinson,
    {{jg}}|John Gilman
{%- endcapture -%}
{%- capture list_of_scores_num -%}
    {{wa}},
    {{bh}},
    {{ag}},
    {{jr}},
    {{jg}}
{%- endcapture -%}

{% assign scores_array = list_of_scores | split: ',' %}
{% assign scores = list_of_scores_num | split: ',' %}
{% assign highest_score = scores | first | plus: 0 %}

{% assign name = '' %}
{% for score_val in scores %}
    {% assign cur_score = score_val | plus: 0 %}
    {% if cur_score >= highest_score %}
        {% assign highest_score = score_val | plus: 0 %}
        {% assign name = scores_array[forloop.index0] | split: '|' | last %}
    {% endif %}
{% endfor %}

<span>{{ highest_score }}</span>
<h5>{{ name }}</h5>

主要区别在于我们将循环外的name值移动为空变量,并在循环内部检查我们使用forloop.index0使用以下{% assign name = scores_array[forloop.index0] | split: '|' | last %}分配名称变量的最大数字代码:

float x = 3.5f; unsigned int i = *((unsigned int *)&x);

所以我们只需要一个循环。