Handlebars JS Example 1
by chridam
HTML
<script src="http://www.fsfoo.com/js/vendor/handlebars-1.0.rc.2.js"></script>
<h1>Handlebars JS Example</h1>
<script id="some-template" type="text/x-handlebars-template"> <table>
<thead>
<th>ID</th>
<th>Section ID</th>
</thead>
<tbody>
{{#items}}
<tr>
<td>{{ItemID}}</td>
<td>{{SectionID}}</td>
</tr>
{{/items}}
</tbody>
</table>
</script>
CSS
body {
font:15px arial,sans-serif;
}
h1 {
margin: 0 0 10px 0;
padding: 5px;
font-size: 24px;
background-color: #999;
color: #fff;
}
table {
margin: 10px;
}
th, td {
padding: 5px;
border: 1px solid #999;
}
th {
background: #ccc;
}
tr:nth-child(odd) {
background: #eee;
}
td a {
color: #000;
text-decoration: underline;
}
JavaScript
var source = $("#some-template").html();
var template = Handlebars.compile(source);
function LoadItems()
{
var data = {
items: [
{
"ItemID": 74,
"SectionID": 4
},
{
"ItemID": 78,
"SectionID": 4
}
]
};
$('body').append(template(data));
}
LoadItems();