[英]how do I get my mustache.js template file included?
I'm working with mustache.js for the first time. 我第一次使用mustache.js。 All the examples I'm finding seem to talk about putting everything inline, but I want my templates in external files so they can be used in multiple places.
我发现的所有示例似乎都在谈论将所有内容放入内联,但我希望我的模板在外部文件中,以便它们可以在多个地方使用。 How do I do that?
我怎么做? (I've got jQuery in my stack, if that makes a difference.)
(我的堆栈中有jQuery,如果这有所不同。)
So say I have: 所以说我有:
template.html template.html
{{title}} spends {{calc}}
data.js data.js
var data = { title: "Joe", calc: function() { return 2 + 4; } };
index.html 的index.html
<script type="text/javascript" src="data.js"></script>
<div id="target"></div>
<script type="text/javascript">
var template = ?????? // how do I attach the template?
var html = Mustache().to_html(template, data);
$('#target')[0].innerHTML = html;
</script>
template = $('.template').val();
Where your template is in the DOM... 您的模板在DOM中的位置...
<textarea class="template">
<h1>{{header}}</h1>
{{#bug}}
{{/bug}}
{{#items}}
{{#first}}
<li><strong>{{name}}</strong></li>
{{/first}}
{{#link}}
<li><a href="{{url}}">{{name}}</a></li>
{{/link}}
{{/items}}
{{#empty}}
<p>The list is empty.</p>
{{/empty}}
</textarea>
You could also render multiple templates directly into your page... 您还可以将多个模板直接渲染到您的页面中......
<script id="yourTemplate" type="text/x-jquery-tmpl">
{{tmpl "#yourTemplate"}}
<div>Something: ${TemplateValue}</div>
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.