I have an array of JSON objects, and I'm trying to figure out how to display them in Mustache.js. An array can be variable in length and content.
Example:
[ Object { id="1219", 0="1219", title="Lovely Book ", url= "myurl} , Object { id ="1220" , 0="1220 , title "Lovely Book2" , url="myurl2"}]
Ive tried:
$.getJSON('http://myjsonurl?type=json', function(data) { var template = $('#personTpl').html(); var html = Mustache.to_html(template, data); $('#test').html(html);
and pattern:
<script id="personTpl" type="text/template"> TITLE: {{#data}} {{title}} IMAGE: {{image}} <p>LINK: <a href="{{blogURL}}">{{type}}</a></p> {{/data}} </script>
but it doesnβt display anything.
I tried to put JSON in an array and then accessed it directly using products[1]
something like this:
$.getJSON("http://myjsonurl?type=json", function(json) { var products = []; $.each(json, function(i, product) { var product = { Title:product.title, Type:product.type, Image:product.image }; products.push(product); ; }); var template = "<h1>Title: {{ Title }}</h1> Type: {{ Type }} Image : {{ Image }}"; var html = Mustache.to_html(template, products[1]); $('#json').html(html); });
which will display one record, but how can I iterate over them and display everything?
source share