当计数器变量的值达到某个数字时,液体标记{%break%}会脱离for循环

时间:2019-02-22 10:32:29

标签: liquid markup

我在这里很新,所以请不要对我害羞:D

我在LM中具有以下代码:

   {% assign count = 0 %}
      {% for insight in contact.insight.Webinisights %}
      {% for page_viewed in insight.page_viewed %}
      {% if page_viewed.custom_page_values.studyTitle != null and page_viewed.custom_page_values.studyTitle != '' %}
      {% assign count = count | plus: 1 %}


          {{ page_viewed.custom_page_values.studyTitle }}
          {{ page_viewed.custom_page_values.studyDescription | append: "..." }}

      {% if count == 2 %}
      {% break %}
      {% endif %}
      {% endif %}
      {% endfor %}
      {% endfor %}

背后的想法是,一旦有人查看了某个页面,该页面的studyTitle!= null和studyTitle!='',我想显示该页面的标题和描述。这可行。该计数器也可以工作,但是我似乎无法弄清楚一旦计数达到2,如何跳出循环,而只是继续进行下去。

2 个答案:

答案 0 :(得分:0)

{% assign count = 0 %}
{% for insight in contact.insight.Webinisights %}
  {% for page_viewed in insight.page_viewed %}
    {% if page_viewed.custom_page_values.studyTitle != null and page_viewed.custom_page_values.studyTitle != '' %}
    {% assign count = count | plus: 1 %}

        {{ page_viewed.custom_page_values.studyTitle }}
        {{ page_viewed.custom_page_values.studyDescription | append: "..." }}

    {% endif %}
    {% if count > 2 %}
    {% break %}
    {% endif %}
  {% endfor %}
{% endfor %}

HTH

答案 1 :(得分:0)

谢谢

与此同时,我设法找到了解决方案。

{% assign mainLoop =  true %}
{% assign count = 0 %}
{% for insight in contact.insight.Webinsight %}
      {% for page_viewed in insight.page_viewed %}
      {% if page_viewed.custom_page_values.studyTitle != null and page_viewed.custom_page_values.studyTitle != ''%}
          {%increment count %}


{{ page_viewed.custom_page_values.studyTitle }}
{{ page_viewed.custom_page_values.studyDescription | append: "..." }}


{% endif %}
{% if count < 2 %}
{% assign mainLoop = false %}

{% break %}
{% endif %}
{% endif %}
{% endfor %}
{% unless mainLoop %}{% break %}{% endunless %}
{% endfor %}