Mustache.js: how can i add external template

2019-06-14 14:29发布

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?

1条回答
我只想做你的唯一
2楼-- · 2019-06-14 15:20
$(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/

查看更多
登录 后发表回答