开发者

Access a mustache template that was loaded in the html head tag?

开发者 https://www.devze.com 2023-02-26 20:58 出处:网络
Is there a way to access the contents of a mustache template file that is loaded via the HTML <head> via javascript?

Is there a way to access the contents of a mustache template file that is loaded via the HTML <head> via javascript?

<link rel="template" href="templates/address.mustache" type="text/html" />

or

<script src="templates/address_field.mustache" type="text/html" charset="utf-8" id="address_template"></sc开发者_如何学运维ript>

I've had success loading them through ajax and through a <script> tag in the body, but I'm not sure how to get the file source when loaded as a separate file through the head.


you need to get method to catch template value external template: below codes only works inside of the html page:

var tmpl=$.trim($('#address_template').val()); //trim the white spaces in the template

Mustache.to_html(tmpl,json);

you need below codes: "$.get('template.html'," is your answer trick. you should load your template firstly:

<!DOCTYPE html>

Titles

<script>
    $(function() {
        $.getJSON('/data/speakers.json', function(data) {
            //var template = $('#speakers-template').html();

           // console.log(template);
           // var info = Mustache.render(template, data);
           // $('#talktitles').html(info);

$.get('template.html', function(template, textStatus, jqXhr) { var info =Mustache.render($(template).filter('#speakers-template').html(), data); $('#talktitles').html(info); });

        });
    });
</script>


Well you need to use a selector that will get the contents of that html.

With jQuery it is as simple as:

var tmpl=$.trim($('#address_template').val()); //trim the white spaces in the template
Mustache.to_html(tmpl,json);
0

精彩评论

暂无评论...
验证码 换一张
取 消