访问加载在html head标签中的mustache模板

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

本文关键字:mustache 模板 标签 head 加载 html 访问      更新时间:2023-09-26

是否有方法访问通过HTML <head>通过javascript加载的胡子模板文件的内容?

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

<script src="templates/address_field.mustache" type="text/html" charset="utf-8" id="address_template"></script>

我已经成功地通过ajax加载它们,并通过在正文中的<script>标签,但我不确定如何获得文件源时,通过头部作为一个单独的文件加载。

你需要get方法来捕捉模板值外部模板:下面的代码只在HTML页面内工作:

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

Mustache.to_html (tmpl、json);

你需要下面的代码:"$.get('template.html',"是你的答案技巧。你应该先加载你的模板:

<!DOCTYPE html>

标题

<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(信息);});

        });
    });
</script>

你需要使用一个选择器来获取html的内容。

使用jQuery,它就像这样简单:

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