Reputation: 15006
I have an array that looks like this:
[
Object
actions: Array[2]
comments: Object
created_time: "2012-06-14T17:45:34+0000"
from: Object
id: "2388163605_10150972634368606"
message: "På jakt efter lift Visby/oskarshamn- Malmö efter almedalsveckan antingen söndagen den 8 juli eller morgon/fm den 9 Juli... Bensinpengar och choklad utlovas... spännande historier kan berättas på begäran."
to: Object
type: "status"
updated_time: "2012-06-14T21:45:15+0000"
__proto__: Object
,
Object
actions: Array[2]
comments: Object
created_time: "2012-06-14T17:45:34+0000"
from: Object
id: "2388163605_10150972634368606"
message: "På jakt efter lift Visby/oskarshamn- Malmö efter almedalsveckan antingen söndagen den 8 juli eller morgon/fm den 9 Juli... Bensinpengar och choklad utlovas... spännande historier kan berättas på begäran."
to: Object
type: "status"
updated_time: "2012-06-14T21:45:15+0000"
__proto__: Object
,
Object
actions: Array[2]
comments: Object
created_time: "2012-06-13T11:30:35+0000"
from: Object
id: "2388163605_10150969775613606"
message: "Har en båtbiljett Nynäshamn-Visby avg 11.05 nu på fredag!"
to: Object
type: "status"
updated_time: "2012-06-13T11:30:35+0000"
__proto__: Object
,
Object
actions: Array[2]
comments: Object
created_time: "2012-05-21T10:36:18+0000"
from: Object
id: "103138046395999_394844430558691"
message: "Hej, är det någon som pendlar Visby-Sthlm-Visby? Jag skulle behöva ha hjälp att få hem en byrå från Sthlm. Den är monterad så den där ganska stor. Så behövs släp,lastbil eller större personbil. Mvh Sabina "
to: Object
type: "status"
updated_time: "2012-05-21T10:36:18+0000"
__proto__: Object
]
I try to itterate and print out every message using mustache. This is the template i try to use:
<ul>
{{#.}}
<li>{{message}}</li>
{{/.}}
</ul>
This is how I post the array into the template:
var template = $('#trips').html();
var content = locationIndex[loc.title];
var html = Mustache.to_html(template, content);
$('#right').html(html);
the template is defined like this:
<script id="trips" type="text/template">
<div id="wrapper">
<ul>
{{#.}}
<li>{{message}}</li>
{{/.}}
</ul>
</div>
</script>
This is a method I've used before, and it seems to work now aswell...
The template works, because the Ul-tag is rendered, but I don't seem to reference the array properly. What am I doing wrong?
Upvotes: 2
Views: 2799
Reputation: 2886
see my response here Mustache JS Template with JSON Collection
data = { 'roles': data }
The below is the json call via web api
$.ajax({
dataType: "json",
url: '/api/TestApi/GetAllRole',
success: function (data) {
`
data = { 'roles': data };
// formatting the data to support the mustache format
var html = Mustache.to_html($('#RoleTemplate').html(), data);
$('#tblRole').append(html);
}
})
Upvotes: 0
Reputation: 185933
Try this:
Template:
<script id="trips" type="text/template">
<div id="wrapper">
<ul>
{{#array}}
<li>{{message}}</li>
{{/array}}
</ul>
</div>
</script>
JavaScript:
var html = Mustache.to_html( template, { array: content } );
I'm assuming that the content
variable contains an array.
Upvotes: 4