Reputation: 749
Hi i am new to this Mustache.js I have a template and js code as below
var template = $('#pageTpl').html();
var html = Mustache.to_html(template, data);
$('#sampleArea').html(html);
and this is template code:
<script id="pageTpl" type="text/template">
<div data-role='page' id='videodiv_{{device_id}}'>
<div data-role=header>
<h1> Device Page </h1>
</div>
<div data-role=content>
<img id='vid_{{device_id}}' src='http://localhost//mjpg/{{device_id}}' width='320'>
</div>
</div>
</script>
this code works fine when i keep the template script in html page. but i want to keep template separately and use it. is there any way to do this?
Upvotes: 0
Views: 4913
Reputation: 22709
$(document).on('pageinit', function() {
$.getJSON('assets/data/channels.json', {}, function(channelData, textStatus, jqXHr) {
var channelList = $('#channels');
$.get('assets/templates/channelList.mustache.html', function(template, textStatus, jqXhr) {
channelList.append(Mustache.render($(template).filter('#channelTpl').html(), channelData))
channelList.listview("refresh");
});
});
});
Source: http://www.levihackwith.com/how-to-load-mustache-js-templates-from-an-external-file-with-jquery/
Upvotes: 4