0

I've been using {{#each}} and {{#if}} in handlebars.js to grab data from JSON files. But I need a way to get the data without any conditions. I have no need for an {{#if}} statement and there's only one set of data so I don't need {{#each}} either. Is there a way to do this? {{#each}} works but I don't want to be using that if there's a simpler method to get a single block of data.

I should note that I'm using Grunt with Handlebars and that my document calls from more than just one JSON file.

Here is my code.

  {{data.PaymentMethod}}
  <table>
  <tr>
    <td>
      <div>Payment Method</div>
      <div>Card Number: {{CardNumber}}</div>
      <div>Card Colder: {{CardHolder}}</div>
      <div><a href="{{Link}}">Tracking</a></div>
    </td>
  </tr>
  </table>

And this is my JSON file (data.json).

{
"PaymentMethod": [
        {
            "CardNumber": "****",
            "CardHolder": "John Doe",
            "Link": "http://www.test.com"
        }
    ]
}
1
  • 1
    How do you call your template? Commented Aug 4, 2016 at 0:59

2 Answers 2

1
<div>Card Number: {{data.PaymentMethod.0.CardNumber}}</div>
<div>Card Colder: {{data.PaymentMethod.[0].CardHolder}}</div>

You should be able to access it like so. Because data.PaymentMethod is an array you have to access the first object in that array.

Sign up to request clarification or add additional context in comments.

6 Comments

Must be missing something as this did not work for me. I'm running Grunt and I got this in my cmd window: Expecting 'ID', got 'INVALID'
ah i was missing out on the . before the array value. the brackets are optional in this case but not if you are using the with helper
The new code no longer throws any errors, but it's not pulling any data in either. :( I've confirmed the filename is right as is the contents of the JSON file. The with helper works for me, but not this. I imagine if I could get your answer to work then that would be ideal because it's less code.
does the with helper work with or without the data part? If it works without have you tried removing the data. from the above code?
{{#with data.PaymentMethod.[0]}} doesn't work but {{#with PaymentMethod.[0]}} does.
|
1

You can use the #with helper to step into the array:

{{#with PaymentMethod.[0]}}
    <div>Payment Method</div>
    <div>Card Number: {{CardNumber}}</div>
    <div>Card Colder: {{CardHolder}}</div>
    <div><a href="{{Link}}">Tracking</a></div>
{{/with}}

3 Comments

I've got multiple JSON files. I noticed your code doesn't specifiy the data.json file. Should there be something in there to make sure it doesn't pull from the wrong JSON file if the PaymentMethod exists elsewhere?
@jimmykup I'm not sure what you mean. What does the data file have to do with the template?
What I mean is that somewhere else in my document I might pull from data2.json. data.json and data2.json might both have PaymentMethod in them (with different data). Your code doesn't explicitly specify to look inside data.json. Would it be best/possible to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.