Liquid - 将数组转换为小写

时间:2017-01-05 05:40:06

标签: shopify liquid

我正在使用Shopify并希望挂钩客户标签,但它们区分大小写。因此{% if customer.tags contains "wholesale" %}{% if customer.tags contains "Wholesale" %}不同。我的客户在应用标签时可能会或可能不会坚持一个案例,因此我希望将来能够防范这种情况。

我想获取一个数组customer.tags,并将所有值转换为小写。我正在尝试解决逻辑,但遇到了麻烦。

我想将customer.tags放入一个无效的新数组中。

{% assign newArray = customer.tags %}
{{ newArray }}

我做错了什么?

3 个答案:

答案 0 :(得分:2)

您可以使用the downcase filter

{% assign contains_wholesale = false %} 

{% for tag in customer.tags %}
  {% assign lowercase_tag = tag | downcase %}
    {% if lowercase_tag == 'wholesale' %}
      {% assign contains_wholesale = true %}
    {% endif %}
{% endfor %}

注意:downcase仅适用于ASCII字符。如果您需要搜索带有重音字母或其他Unicode字符的字符串,那么这还不够。

答案 1 :(得分:2)

使用"另一种解决方案包含"运营商将跳过" w"。

像{%if customer.tags包含' holesale' %} 应该管用。

答案 2 :(得分:2)

如果您想将customer.tags保留为数组,以便可以在简单的contains语句(如您的示例)中继续使用if。您还可以将几个液体过滤器链接在一起,以将数组中的所有字符串变成小写。

示例:

  {% assign lowercaseTags = customer.tags | join: ',' | downcase | split: ',' %}
  {% assign randomString = 'WholeSale' | downcase %}

  {% if lowerCaseTags contains randomString %}
    {% comment %}
    Will now match regardless of case sensitivity
    {% endcomment %}
  {% endif %

说明:

相关问题