Shopify / Liquid:将一组标签分开,然后将它们放在选定的组件上

发布于 2025-02-08 16:26:27 字数 942 浏览 2 评论 0 原文

我有大量的标签,我希望能够将它们分配。例如,我有 green blue red black code> coloreless 白色标签(以及其他一些没有颜色的标签,所以我不会把它们放在这里),我想将它们放在数组中。 我尝试了这样的事情:

{% assign colors = "" | split: '' %}
{% for tag in collection.all_tags %}
    {% if tag == 'Black' or tag == 'Blue' or tag == 'Colorless' or tag == 'Red' or tag == 'White' %}
        {% assign comma = "," %}
        {% assign color = tag | split: '_' %}
        {% assign colors = colors | concat: color %}
        {% assign colors = colors | concat: comma %}
    {% endif %}
{% endfor %}

但是,这使我 BlackBlueGreenredColorlessWhite ,没有任何空间。另外,我是否可以将这些选定的标签放在< select> 组件中,以便我可以通过选定的标签过滤我的产品?就像在而不是< ul>

I have a large set of tags, and I want to be able to sepparate them. For example, I have the Green, Blue, Red, Black, Colorless and White tags (as well as some other tags that aren't colors, so I won't put them here) and I want to place them in an array.
I tried with something like this:

{% assign colors = "" | split: '' %}
{% for tag in collection.all_tags %}
    {% if tag == 'Black' or tag == 'Blue' or tag == 'Colorless' or tag == 'Red' or tag == 'White' %}
        {% assign comma = "," %}
        {% assign color = tag | split: '_' %}
        {% assign colors = colors | concat: color %}
        {% assign colors = colors | concat: comma %}
    {% endif %}
{% endfor %}

However, that returns me BlackBlueGreenRedColorlessWhite, without any spaces whatsoever. Also, is there a way I can place those selected tags in a <select> component so I can filter my products by the selected tag? Like in the example provided in this page but using a <select> instead of a <ul>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2025-02-15 16:26:27

首先,通过具有标签来对您的标签进行分类;

IE:色蓝色,颜色红色,颜色绿色等。

为什么?您无需添加更多IF-ELSE语句;

然后,在下面的代码下,您将获得一系列颜色。

{% assign color = "" %}
{% for tag in collections.all_tags %}
    {% if tag contains 'color-' %}
        {% assign trimmed = tag | split: "-" | last %}
        {% assign color = color | append: trimmed | append: ", " %}
    {% endif %}
{% endfor %}

{% assign color = color | split: ", " %}

<select name="color" id="color">
    {% for x in color %}
        <option value="{{x}}">{{x}}</option>
    {% endfor %}
</select>

first, categorizes your tag by having a label;

i.e: color-blue, color-red, color-green, etc.

why? You will not need to add more if-else statement;

then, with code below, you will get an array of color;

{% assign color = "" %}
{% for tag in collections.all_tags %}
    {% if tag contains 'color-' %}
        {% assign trimmed = tag | split: "-" | last %}
        {% assign color = color | append: trimmed | append: ", " %}
    {% endif %}
{% endfor %}

{% assign color = color | split: ", " %}

<select name="color" id="color">
    {% for x in color %}
        <option value="{{x}}">{{x}}</option>
    {% endfor %}
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文