如何缓存胡子模板?
我想缓存 mustache
模板。
我知道我可以直接包含 mustache
模板,如下所示:
<script id="mustache-template" type="text/html">
<h1>{{title}}</h1>
</script>
并使用 javascript 调用这些模板,如下所示:
var html, template, data;
data = {
title : "Some title"
};
template = document.getElementById('mustache-template').innerHTML;
html = Mustache.to_html(template, data);
这不会缓存模板。我能弄清楚的唯一方法是使用链接标签,但是如何在没有ajax
请求的情况下通过javascript
调用模板内容?
这赢了不起作用(当然)...
HTML
<link type="text/html" href="/mustache/template.tpl" id="mustache-template" />
Javascript
document.getElementById('mustache-template').innerHTML;
I would like to cache mustache
templates.
I know that I could include mustache
templates directly, like this:
<script id="mustache-template" type="text/html">
<h1>{{title}}</h1>
</script>
And call those with javascript, like this:
var html, template, data;
data = {
title : "Some title"
};
template = document.getElementById('mustache-template').innerHTML;
html = Mustache.to_html(template, data);
This won't cache templates. Only way I could figure out is usage of link -tags, but how do I call template content via javascript
without an ajax
request?
This won't work (of course)...
HTML
<link type="text/html" href="/mustache/template.tpl" id="mustache-template" />
Javascript
document.getElementById('mustache-template').innerHTML;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题很有趣!几个月前,当我开始在 Rails 项目中使用 Mustache 进行“巨大”前端模板时,我遇到了同样的问题。
我最终得到了以下解决方案...
小胡子模板位于公共文件夹内:
每当我需要一个模板时,我都会有这个帮助程序 getTemplate 来执行一些操作(有一些 mootools,但也有注释):
我以这种方式调用这个帮助程序:
您可以注意到第一次用户需要模板中,有一个异步请求(如果您不想在回调中包含其他代码,您可以发出同步请求)
我希望它能有所帮助,并且我很乐意收到有关此内容的反馈/建议:)
This question is very interesting! I had the same problem several months ago when I started to use mustache for 'huge' front-end templating within a rails project.
I ended up with the following solution...
Mustache templates are inside a public folder :
Whenever I need a template I have this helper getTemplate that does some stuff (there's some mootools, but there are comments too):
and I call this helper in this way :
You can notice that first time user needs the template, there's an asynch request (you could make a sync request if u don't want to wrap some other code inside the callback)
I hope it could help and I'd love to receive feedbacks/suggestions concerning this stuff :)
您可以尝试在
iframe
中加载模板,其中包含一个 HTML 页面(将被缓存),其中包含所有script
标记。然后您可以从主页读取它们,或将它们从 iframe 推送到
父
窗口。这就是我在使用 pure.js 模板时所做的事情
You could try to load your template in an
iframe
that contains an HTML page(that will be cached) with all yourscript
tags inside.Then you can read them from the main page, or push them from the iframe to the
parent
window.That is what I do when using pure.js templates
当然,你所说的不会起作用,因为 liknek 元素的innerHTML 属性不会给你链接的内容。
您可以使用 Chevron 从链接加载外部模板,如下所示:
您在模板中添加链接到你的模板文件:
然后,在你的 JS 中,你可以像这样渲染你的模板:
Chevron 的文档将给出更多示例
What you say it will not work, of course, because the innerHTML attribute of the liknek element will not give you the contents of the link.
You can use Chevron to load external templates from links like so:
You add in you template a link to your template file:
Then, in you JS you can render your template like so:
The docs of Chevron will give more examples