我尝试返回所有product.custom_fields
,除非名称字段包含“特征、宽度、高度或深度”。
如果按一个值进行过滤,我可以使用以下两种代码变体来使逻辑正常工作:
{{#each product.custom_fields}}
{{#unless (occurrences name 'Height')}}
{{else}}
<tr>
<th>{{ name }}</th>
<td>{{ sanitize value }}</td>
</tr>
{{/unless}}
{{/each}}
和
{{#each product.custom_fields}}
{{#startsWith 'Feature' name}}
{{else}}
{{#if value '!==' 'true'}}
<tr>
<th>{{ name }}</th>
<td>{{ sanitize value }}</td>
</tr>
{{/if}}
{{/startsWith}}
{{/each}}
但是当尝试对多个相同的内容时它会崩溃。例如
{{#unless (occurrences name 'Height' occurrences name 'Width' occurrences name 'Depth' occurrences name 'Feature')}}
Handlebars 是我刚刚开始使用的东西,因此任何尝试进行更复杂过滤的建议/最佳实践将不胜感激:)
最佳答案
Unless、StartsWith、If、Contains 全部只看单个项目。要将多个项目分组,您实际上需要使用不同的帮助器:and、or、all、any。
一个例子:
{{#each product.custom_fields}}
{{#any (occurrences name 'Height') (occurrences name 'Width') (occurrences name 'Depth') (occurrences name 'Feature')}}
{{else}}
<tr>
<th>{{ name }}</th>
<td>{{ sanitize value }}</td>
</tr>
{{/any}}
{{/each}}
关于handlebars.js - 如何在 Big Commerce 中按名称字段中的多个值过滤 Product.custom_fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71501840/