Kermani Kermani - 6 months ago 18
Javascript Question

Pass through JSON array using Handlebars template engine

This is my JSON and I want to know how can I show my information into page using handlebars template engine:

This is my template (script):

<script id="template-app" type="text/x-handlebars-template">
{{#each data}}
Email: {{email}} <br/>
First Name: {{first_name}} <br/>
Last Name: {{last_name}} <br/>
{{/each}}
</script>


And I send an ajax request to get the following JSON:

$(function(){
var theTemplateScript= $("#template-app").html();
var theTemplate= Handlebars.compile(theTemplateScript);

$.ajax({
url: 'http://localhost/cb/MOCK_DATA.json',
type: 'GET',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function(data) {
var theCompiledHTML= theTemplate(data);
$(document.body).append(theCompiledHTML);

}
});
});


This is the JSON that above Ajax request get:

[{"first_name":"Betty","last_name":"Sims","email":"bsims0@studiopress.com"},
{"first_name":"Roger","last_name":"Mendoza","email":"rmendoza1@google.pl"},
{"first_name":"Albert","last_name":"West","email":"awest2@cornell.edu"},
{"first_name":"Bobby","last_name":"Lane","email":"blane3@ameblo.jp"},
{"first_name":"Julie","last_name":"Wheeler","email":"jwheeler4@google.ru"}]


It is not working, and I believe it is from template that I wrote!

Answer

It is because you are saying for each data, loop through the array. However, you are passing in a plain old array to the Handlebar template. It is expecting an object with property data and an array value. So you could change your Handlebars template to this: -

<script id="template-app" type="text/x-handlebars-template">
    {{#each this}}
        Email: {{email}} <br/>
        First Name: {{first_name}} <br/>
        Last Name: {{last_name}} <br/>
    {{/each}}
</script>

Or alternatively, you could adapt the JSON data to work with your existing Handlebars template, like so: -

var json = {
    data: [{
        "first_name": "Betty",
        "last_name": "Sims",
        "email": "bsims0@studiopress.com"
    }, {
        "first_name": "Roger",
        "last_name": "Mendoza",
        "email": "rmendoza1@google.pl"
    }, {
        "first_name": "Albert",
        "last_name": "West",
        "email": "awest2@cornell.edu"
    }, {
        "first_name": "Bobby",
        "last_name": "Lane",
        "email": "blane3@ameblo.jp"
    }, {
        "first_name": "Julie",
        "last_name": "Wheeler",
        "email": "jwheeler4@google.ru"
    }]
};

And for your JavaScript code, you just need a little change to get the above JSON:

$(function(){
    var theTemplateScript= $("#template-app").html();
    var theTemplate= Handlebars.compile(theTemplateScript);
    $.ajax({
        url: 'http://localhost/cb/MOCK_DATA.json',
        type: 'GET',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            var json_handlbars={
                data:result
            };
            var theCompiledHTML= theTemplate(json_handlbars);
            alert(theCompiledHTML);
            $(document.body).append(theCompiledHTML);

        }
    });
});