有没有更好的方法来检查非字母字符?

时间:2017-01-11 20:31:50

标签: shopify liquid

我正在shopify上创建一个页面,列出我们所有的供应商,并按字母顺序对它们进行分组。我希望所有以非字母字符开头的供应商都归入#。

这就是我提出的,有更好的方法吗?

<div id="designers-a-to-z" class="vendor-list">
    <ul>
        {% assign current = "" %}
        {% for product_vendor in shop.vendors %}
            {% assign first_letter = product_vendor | strip | upcase | slice: 0 %}
            {% if first_letter != "A" and first_letter != "B" and first_letter != "C" and first_letter != "D" and first_letter != "E" and first_letter != "F" and first_letter != "G" and first_letter != "H" and first_letter != "I" and first_letter != "J" and first_letter != "K" and first_letter != "L" and first_letter != "M" and first_letter != "N" and first_letter != "O" and first_letter != "P" and first_letter != "Q" and first_letter != "R" and first_letter != "S" and first_letter != "T" and first_letter != "U" and first_letter != "V" and first_letter != "W" and first_letter != "X" and first_letter != "Y" and first_letter != "Z" %}
                {% assign first_letter = "#" %}
            {% endif %}
            {% unless first_letter == current %}
                <h3><span id="designers-{{ first_letter }}" class="anchor"></span>{{ first_letter }}</h3>
            {% endunless %}
            <li class="vendor-list-item">
                <a href="/collections/{{ product_vendor | handleize }}">{{ product_vendor }}</a>
            </li>
            {% assign current = first_letter %}
        {% endfor %}
    </ul>
</div>

编辑:

我能够使用此代码(仍然想知道是否有更有效的方法来执行此操作):

<div class="vendor-list" id="designers-a-to-z">
    <ul>
        {% assign current = "" %}
        {% capture alphabet %}
            -A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z
        {% endcapture %}
        {% assign letters = alphabet | split: '-' %}
        {% assign its_a_letter = false %}
        {% for vendor in shop.vendors %}
            {% assign vendor_first_letter = vendor | strip | upcase | slice : 0 %}
            {% for letter in letters %}
                {% if vendor_first_letter == letter %}
                    {% assign its_a_letter = true %}
                    {% break %}
                {% endif %}
            {% endfor %}
            {% if its_a_letter %}
                {% unless vendor_first_letter == current %}
                    <h3><span class="anchor" id="designers-{{ vendor_first_letter }}"></span>{{ vendor_first_letter }}</h3>
                {% endunless %}
                <li class="vendor-list-item">
                    <a href="/collections/{{ vendor | handleize }}">{{ vendor }}</a>
                </li>
                {% assign current = vendor_first_letter %}
            {% else %}
                {% assign vendor_first_letter = "#" %}
                {% unless vendor_first_letter == current %}
                    <h3><span class="anchor" id="designers-{{ vendor_first_letter }}"></span>{{ vendor_first_letter }}</h3>
                {% endunless %}
                <li class="vendor-list-item">
                    <a href="/collections/{{ vendor | handleize }}">{{ vendor }}</a>
                </li>
                {% assign current = vendor_first_letter %}
            {% endif %}
        {% endfor %}
    </ul>
</div>

1 个答案:

答案 0 :(得分:1)

解决方法可能是使用过滤器'sort_natural'直接自动对数组进行排序。

例如,使用此:

{% assign vendor_list = "#doe1, Zig, John, Betty" | split: ", " %}
{{ vendor_list | sort_natural | join: ", " }}

将显示:

#doe1, Betty, John, Zig