Skip to content

Instantly share code, notes, and snippets.

@Encosia
Created August 18, 2011 03:11
Show Gist options
  • Save Encosia/1153207 to your computer and use it in GitHub Desktop.
Save Encosia/1153207 to your computer and use it in GitHub Desktop.
Remote template loading with jQuery Templates
<script id="invoiceTemplate" type="x-jquery-tmpl">
<table class="invoice">
<thead>
<tr>
<th>Service/Item</th>
<th>Price</th>
<th>Qty</th>
</tr>
</thead>
<tbody>
{{each lineItems}}
{{tmpl($value) get_invoiceRowTemplateName(type)}}
{{/each}}
</tbody>
</table>
</script>
<script type="text/javascript">
function get_invoiceRowTemplateName(type) {
return '#' + type + 'RowTemplate';
}
</script>
<!-- COMMENT! -->
<script id="serviceRowTemplate" type="x-jquery-tmpl">
<tr class="service">
<td>${service}</td>
<td>${price}</td>
<td>${qty}</td>
</tr>
</script>
<script id="itemRowTemplate" type="x-jquery-tmpl">
<tr class="item">
<td>${item} - ${description}</td>
<td>${price}</td>
<td>${qty}</td>
</tr>
</script>
<html>
<head>
<title>Composite template</title>
<style>
table.invoice {
width: 600px;
border: 1px solid #999;
}
</style>
</head>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>
<script type="text/javascript">
var invoice = {
lineItems: [
{ type: 'item', item: '99Designs', description: '99 Designs logo', price: 250.00, qty: 1 },
{ type: 'service', service: 'Web development and testing', price: 25000.00 },
{ type: 'item', item: 'Linode12Month', description: 'Linode hosting, 1 year', price: 29.99, qty: 12 }
]
};
$.get('_invoice.tpl.htm', function(templates) {
$('body').append(templates);
$('#invoiceTemplate').tmpl(invoice).appendTo('body');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment