handlebars.js - 如何在 Big Commerce 中按名称字段中的多个值过滤 Product.custom_fields

标签 handlebars.js bigcommerce

我尝试返回所有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/

相关文章:

php - Bigcommerce 客户登录api(单点登录)无效登录问题

javascript - 将 Big Commerce 更新到新的 Analytics.js

javascript - 预编译 Handlebars 模板以使其发挥作用的正确方法,就像 Jade 那样

javascript - 通过Helper访问全局变量

javascript - 无法提供模板

javascript - Handlebars 表达式中的连接变量

html - 如何使用应用程序开发 API 将动态 html 或脚本添加到 Bigcommerce 商店中的任何页面

javascript - 无法在 owl-carousel 选项数组中写入 var 值

optimization - BigCommerce 和开发/优化

javascript - 如何在 hbs 模板中显示数据?