ruby – 如果产品标签包含 – 在shopify中
所以我基本上试图使用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 %} 但是,每次查询返回相关产品时,这将显示图像. 它后面是什么(图像)(查询结果) – 如果没有查询结果,那么它什么都不显示. 有任何想法吗? 解决方法
你的第一段代码无效的原因是因为contains正在寻找一个名为’related’的标签,而不是一个包含子字符串’related’的标签.
请参阅其声明的Shopify Wiki for contains:
在您的实例中,contains正在检查简单字符串数组中的字符串(并且正在查找整个字符串,而不是包含指定字符串作为子字符串的字符串). 另见Shopify wiki for product.tags:
所以,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 %} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |