2

I'm trying to create a generic template which will render out JSON objects. The problem is, with all the samples I have seen, they are based on knowing the names of the key...

I started off trying something like this:

<table>
    {{each}}
    <tr>
        <td>$($value[0]}</td><td>$($value[1]}</td>
    </tr>
    {{/each}}
</table>

Now I this isn't generic yet but I was trying this as a start, but it doesn't work...

2 Answers 2

1

This was not apparent to me either.

<table>
     <tr>
        {{each $data}}
        <td>${ $index }</td><td>${ $value }</td>
        {{/each}}
     </tr>
</table>

But when you see it you have a D'OH moment.

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

Comments

0

Here's some code that will get you the keys and values of your JSON object. I'm basing this off of jQuery JSON Associative Array.

for(var key in obj) {
  if (obj.hasOwnProperty(key)){
    document.write("<tr><td>" + key + "</td><td>" + obj[key] + "</td></tr>");
  }
}

It's standard javascript. There's an shorter way to do this in jQuery (mentioned in the previous link).

1 Comment

I know i could do this, but the idea is that its supposed to be using a template as there are other factors at work here...

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.