【问题标题】:If product tags contains - in shopify如果产品标签包含 - 在 shopify
【发布时间】:2013-09-16 06:29:09
【问题描述】:

所以如果产品的标签包含“相关”字样,我基本上会尝试使用shopify的逻辑来显示图像

(有一个 json 查询可以获取包含“related-x”的标签,其中 x 是产品的名称,它使用它来显示相关产品。)

在 Json 查询之前是一张基本上表示“相关产品”的图像。我想做的是仅在存在“相关”标签时才显示此内容。

我试过这个:

{% if product.tags contains 'related' %}              
          <img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}

不显示任何内容。我也试过:

{% for t in product.tags %}
{% if t contains 'related-' %}
<img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}
{% endfor %}

但是,每次查询返回相关产品时,这都会显示图像。

我追求的是它去(图片)(查询结果) - 如果没有查询结果,那么它什么也不显示。

有什么想法吗?

【问题讨论】:

  • 如果你使用product.tags.join contains 'related'?在我看来 tags 是一个数组。 contains 在字符串中查找子字符串;不在数组中。 join 将数组转换为字符串 wiki.shopify.com/Join

标签: ruby shopify liquid


【解决方案1】:

您的第一段代码不起作用的原因是contains 正在寻找一个名为“相关”的标签,而不是包含子字符串“相关”的标签。

查看Shopify Wiki for contains 的声明:

它可以检查一个字符串是否存在于另一个字符串中,也可以检查一个字符串是否存在于一个简单字符串数组中。

在您的实例中,contains 正在检查简单字符串数组中的字符串(并且正在查找整个字符串,而不是包含指定字符串作为子字符串的字符串)。

另见Shopify wiki for product.tags

返回产品标签列表(由​​简单字符串表示)。

您可以将 contains 关键字与简单字符串数组一起使用,因此 您可以将其与产品标签一起使用:

{% if product.tags contains 'custom to order' %}
&lt;!-- Output custom to order form HTML here --&gt;
{% endif %}

所以,Gerard Westerhof 建议在上面的评论中使用Join 是一个很好的建议。如果您首先加入product.tags 数组,那么contains 将在join 返回的标签字符串中搜索“相关”字符串。

试试这个:

{% if product.tags | join: ' ' contains 'related' %}
    <img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}

编辑:

试试这个:

{% assign product_tags_string = product.tags | join: ' ' %}
{% if product_tags_string contains 'related' %}
    <img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}

【讨论】:

  • 感谢您的介绍。我试过了,现在无论是否有“相关”标签,它总是显示 gif。也许有必要做负面的事情?所以 - 如果 product.tags 不包含相关的,那么什么都没有 > 否则显示 gif?你怎么看?
  • @user2782857 嗯,这很奇怪。似乎 {% if product.tags | join ' ' contains 'related' %} 没有按照您期望的顺序进行评估。在product.tags | join ' ' 周围添加括号也没有任何影响,但是在使用contains 之前将其分配给变量对我有用。请参阅上面的编辑。
  • Holy fking st Steph,你是凡人中的神。我欠你大约 200 杯啤酒什么的
  • 对于那些复制代码的人。使用带有冒号的| join:' '
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多