3

I have following data in json

"ICON":{
    "144":"https://example.com/bubble-academy.jpg",
    "228":"https://example.com/bubble-academy.jpg",
    "72":"https://example.com/bubble-academy.jpg",
    "152":"https://example.com/bubble-academy.jpg",
    "130":"https://example.com/bubble-academy.jpg",
    "120":"https://example.com/bubble-academy.jpg",
    "32":"https://example.com/bubble-academy.jpg"
}

In handlebars, I am trying to access property 32 like following.

<img src="{{ ICON.32 }}">

and I get following error

Module build failed: Error: Parse error on line 5:
..."{{ mediaFiles.ICON.32  }}">        <sp
-----------------------^
Expecting 'ID', got 'NUMBER'

How can I solve this problem?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Om3ga
  • 30,465
  • 43
  • 141
  • 221

2 Answers2

6

This is because you are using a number as the property name, try using this instead:

<img src="{{ ICON.[32] }}">

0

Here is also an interesting case to confuse people: (this is how statically generated wordpress forms look)

Input
{
    "body" : {
        'input_2.2': 'Mrs.',
        'input_2.3': 'Cynthia',
        'input_2.6': 'Winterbottom',   
        'input_6': 'Professor',
  }
}

This template results in blanks

<p>Form submission</p>
  <p>{{body.input_2.[2]}} {{body.input_2.[3]}} {{body.input_2.[6]}}</p>
  <p>{{body.input_6}}</p>

Result:
<p></p>
<p>Professor</p>

Apparently since all of the numeric entries do not exist... you can not simply bracket them... you must group the whole subpath at once

Proper template:
<p>Form submission</p>
  <p>{{body.[input_2.2]}} {{body.[input_2.3]}} {{body.[input_2.6]}}</p>
  <p>{{body.input_6}}</p>

This was found via trial and error... could not find proper documentation explaining this