每创建3列后添加新行

2022-03-02 00:00:00 for-loop backend shopify php

在Shopify产品页面中,添加了一个新部分,以便在一个3列的行中显示一些统计数据。我试着用下面的代码在3次迭代后将每个新列放在新的一行中。我希望行只有3列。这似乎与我的逻辑不符。请帮助我找出为什么第三次迭代后的列没有出现在新行中。

<h2>Our Gifts Create an Impact</h2>
<div class="impact-items">
 {% for block in section.blocks %}
  <div class="impact-content col-xs-12 col-md-4">
    <div class="impact-icon"><img src="{{block.settings.impact_icon | img_url: 'master' }}"></div>
    <div>
     <div class="impact-number">{{block.settings.impact_number}}</div>
     <div>{{block.settings.impact_text}}</div>
   </div>
  </div>
 {% endfor %}
</div>

<div class="mt2x btn center-xs col-sm-12 col-md-12 col-lg-12 col-xl-12"><a href='https://packedwithpurpose.gifts/our-impact/impact-report-2020/'><button>Learn More About our Impact</button></a></div>

{% schema %}
 {
  "name": "Impact Stats",
  "blocks" : [
   {
    "type":"icon_number_text",
    "name":"Stats Block",
    "settings" : [
     {
      "id":"impact_icon",
      "type":"image_picker",
      "label":"Impact Icon"
     },
     {
      "id":"impact_number",
      "type":"text",
      "label":"Impact Number"
     },
     {
      "id":"impact_text",
      "type":"text",
      "label":"Impact Text"
     }
    ]
   }
  ]
 }
{% endschema %}

{% stylesheet %}
.product-view .impact-section h2 {
 align-content: center;
 text-align: center;
 color: #847d7a;
 font-family: "Playfair Display",serif !important;
 text-transform: none;
 letter-spacing: normal;
 font-weight: 700 !important;
 font-style: italic;
 margin: 30px 0;
}

.impact-content {
 display: flex;
 align-items: center;
}

.impact-items {
 display:flex;
}

.impact-section .mt2x.btn.center-xs.col-sm-12.col-md-12.col-lg-12.col-xl-12 button {
 background-color: #be9475;
 border-radius: 50px;
 background-color: #be9475;
 border-radius: 50px;
 height: 70px;
 line-height: 3;
 font-weight: 700;
 font-size: 20px;
}

.impact-icon {
 padding-right:16px;
}

.impact-number {
 font-weight:900;
 font-size:25px;
}
{% endstylesheet %}

{% javascript %}
{% endjavascript %}

div

这里是液体代码的代码片段,它在3个项目循环后添加并关闭推荐答案,您可以根据需要检查和增强逻辑

{% assign defaultSize = 3 %} // this the items after div is closed
{% assign counter = 1 %}
{% assign count = 1 %}
{% for i in (1..10) %}
    {% assign nextItem = count | minus:1 %}
    {% assign nextItem = defaultSize | times:nextItem %}
    {% assign nextItem = nextItem | plus:1 %}
    {% if counter == 1 or counter == nextItem %}<div class="row">{% endif %}
        <div class="cls-{{forloop.index}}">{{forloop.index}}---{{counter}}</div>
    {% assign lastItem = defaultSize | times:count %}
    {% if counter == lastItem or forloop.last == true %}
        </div>
        {% assign count = count | plus: 1 %}
    {% endif %}
    {% assign counter = counter | plus: 1 %}
{% endfor %}

以下是我的开发存储上的以下代码片段的输出:

相关文章