如何缩短此液体代码?

时间:2015-03-20 19:14:04

标签: shopify liquid

我想更新"新订单通知"使用自定义HTML代码为我的Shopify商店发送邮件,但在粘贴代码时,它根本不会粘贴100%的代码。我已联系Shopify,他们告诉我没有字符限制,但文件大小限制为64或128 kb。

该商店有18个主要类别(收藏品)。我要做的是确保根据类别名称对产品进行排序。

以下代码在自定义模板中出现18次(对于每个类别):

{% if verse_kantenklaar_maaltijden_salades != blank %}
  <tr>
    <td colspan="2">
      <h2 style="margin-bottom:15px;margin-top:5px!important;font-weight:bold;">Verse kant-en-klaar maaltijden - salades</h2>
    </td>
  </tr>

  {% for line_item_id in verse_kantenklaar_maaltijden_salades %}
      {% for line_item in line_items %}
          {% capture line_item_string %}{{line_item.id}}{% endcapture %}
          {% if line_item_id == line_item_string %}

            {% assign bonus = 'no' %}

            {% for tag in line_item.product.tags %}
              {% if tag == 'Bonus' %}
                {% assign bonus = 'yes' %}
              {% endif %}
            {% endfor %}

            <tr>
              <td>
                <a href="{{ line_item.product.featured_image | product_img_url: 'master' }}"><img alt="" src="{{ line_item.product.featured_image | product_img_url: 'large' }}" style="width: 400px;"></a>
              </td>

              <td style="text-align:left;">{{ line_item.title }} ({{ line_item.product.metafields.global.item_size }})<br>
                  <span style="font-size:34px;font-weight:bold;">
                    {% if bonus == 'yes' %}
                      {{ line_item.product.metafields.global.wpob | round: 2 }}
                    {% else %}
                      {{ line_item.product.metafields.global.wpo | round: 2 }}
                    {% endif %}
                  </span>

                  {% if bonus == 'yes' %}
                    <span style="font-size:18px;font-weight:bold;">BONUS</span>
                  {% endif %}

                  {% if line_item.quantity > 1 %}<span class="item_count" style="display: block;position: relative;width: 50px;text-align: center;background-color: #d14836;color: white;font-weight: 700;min-width: 80px;line-height: 40px;border-radius: 50px;">{{ line_item.quantity }}</span>{% endif %}
              </td>
            </tr>

          {%endif%}
      {% endfor %}
  {% endfor %}
{% endif %}

有关完整代码(包含3个类别),请查看this gist

有没有办法简化(使用循环)或压缩它以确保总模板保持在Shopify限制范围内?

或者这可能是由其他原因造成的?总代码长度约为1500行。

1 个答案:

答案 0 :(得分:0)

对于任何有兴趣的人,这是更有效的版本(感谢Evulse):

{% assign sort_array = "" %}

{% for line_item in cart.items %}
    {% for tag in line_item.product.tags %} 
        {% if tag == 'Tag I want second' %}
            {% assign sort_array = sort_array | append: 'B:' | append: line_item.id | append: ',' %}
        {% endif %}
        {% if tag == 'Tag I want First' %}
            {% assign sort_array = sort_array | append: 'A:' | append: line_item.id | append: ',' %}
        {% endif %}  
        {% if tag == 'Tag I want Third' %}
            {% assign sort_array = sort_array | append: 'C:' | append: line_item.id | append: ',' %}
        {% endif %} 
    {% endfor %}
{% endfor %}

{% assign sort_array = sort_array | split: ',' | sort %}

{% for prefixed_line_item_id in sort_array %}
    {% for line_item in cart.items %}
        {% capture line_item_string %}{{line_item.id}}{% endcapture %}
        {% assign line_item_id = prefixed_line_item_id | split: ':' | last %}
        {% if line_item_id == line_item_string %} 
            {{line_item.product.title}}
        {%endif%}
    {% endfor %}
{% endfor %}
相关问题