简体   繁体   中英

Bigcommerce Stencil Handlebars helper 'inArray'

I'm attempting to use the Bigcommerce Handlebars helper 'inArray'. I interpreted the helper as being able to find a particular value in an array. I can get my else statement to work but it shouldn't be outputting anything since the value I'm looking for does exist.

From the docs:

{{inArray}} Block helper that renders the block if an array has the given value. Optionally, you can specify an inverse block to render when the array does not have the given value.

Parameters:

  • array {Array}
  • value {any}
  • options {Object}
  • returns {String}

My sample 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"
      }]

My Handlebars:

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

You could try using {{#each}} and then an if/else statement to check for the value. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM