【问题标题】:Bigcommerce Stencil Handlebars helper 'inArray'Bigcommerce Stencil Handlebars 助手“inArray”
【发布时间】:2018-03-27 15:37:44
【问题描述】:

我正在尝试使用 Bigcommerce Handlebars 助手“inArray”。我将帮助程序解释为能够在数组中找到特定值。我可以让我的 else 语句工作,但它不应该输出任何东西,因为我正在寻找的值确实存在。

来自文档:

{{inArray}} 如果数组具有给定值,则呈现块的块助手。或者,您可以指定在数组没有给定值时呈现的逆块。

参数:

  • 数组{数组}
  • 值{任何}
  • 选项 {对象}
  • 返回 {String}

我的示例 json:

"custom_fields": [
      {
        "id": "41005235",
        "name": "room",
        "value": "Kitchen"
      },
      {
        "id": "41005236",
        "name": "REQUIRED (NOT INCLUDED)",
        "value": "1 LED 13 WATT BULBS"
      },
      {
        "id": "41005237",
        "name": "FINISH",
        "value": "BRONZE W/POLISHED BRASS ACCENTS"
      }]

我的车把:

{{#inArray custom_fields "FINISH"}}
<h1>it's there</h1>
{{else}}
<h1>it's not there</h1>
{{/inArray}}

【问题讨论】:

  • inArray 助手将查找“FINISH”字符串,但它不会查找数组中的对象。您最好构建自己的助手或使用类似this
  • 不幸的是,我不允许构建自己的助手。来自文档:出于安全原因,BigCommerce 不允许 Stencil 开发人员定义新的自定义 Handlebars 助手——您必须使用当前可用的助手。但是,您可以通过对 Stencil Github 存储库的拉取请求来建议新的自定义助手。

标签: handlebars.js bigcommerce


【解决方案1】:

您可以尝试使用 {{#each}} 然后使用 if/else 语句来检查值。例如:

{{#each product.custom_fields}}
    {{#if name '===' 'FINISH'}}
    <h1>It's there</h1> 
          {{else}}
    <h1>It's not there</h1>
    {{/if}}
{{/each}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多