如何缓存胡子模板?

发布于 2024-11-14 19:38:27 字数 833 浏览 3 评论 0原文

我想缓存 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄离 2024-11-21 19:38:28

这个问题很有趣!几个月前,当我开始在 Rails 项目中使用 Mustache 进行“巨大”前端模板时,我遇到了同样的问题。

我最终得到了以下解决方案...


小胡子模板位于公共文件夹内:

<代码>/public/templates/_template_name.tpl

每当我需要一个模板时,我都会有这个帮助程序 getTemplate 来执行一些操作(有一些 mootools,但也有注释):

// namespace.templatesCache is an object ( {} ) defined inside the main app js file

var 
    needXHR = false, // for callback function
    templateHTML = ""; //template html

if(!(templateHTML = namespace.templatesCache[template_name])){ //if template is not cached

    templateHTML = (this.helpers.supportLocalStorage) ? localStorage.getItem(template_name) : ""; //if browser supports local storage, check if I can retrieve it

    if(templateHTML === "" || templateHTML === null){ // if I don't have a template (usually, first time), retrieve it by ajax

      needXHR = true;  

      new Request.HTML({ //or jQuery's $.get( url /*, etc */ ) 

          url: namespace.URLS.BASE+"templates/_"+template_name+".tpl", // url of the template file

          onSuccess : function(t, e, html, js){

                namespace.templatesCache[template_name] = html; //cache it

                if(_this.helpers.supportLocalStorage){ //and store it inside local storage, if available
                  localStorage.setItem(template_name,html); 
                }

                //call callback      
          }
      }).get();

    }else{ //retrieved by localStorage, let's cache it

        namespace.templatesCache[template_name] = templateHTML;

    }

}

if(!needXHR){ // I retrieved template by cache/localstorage, not by Ajax

    //call callback    

}

我以这种方式调用这个帮助程序:

namespace.helpers.getTemplate('template_name', function( templateHTML ){
    // the callback function
});

您可以注意到第一次用户需要模板中,有一个异步请求(如果您不想在回调中包含其他代码,您可以发出同步请求)

我希望它能有所帮助,并且我很乐意收到有关此内容的反馈/建议:)

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 :

/public/templates/_template_name.tpl

Whenever I need a template I have this helper getTemplate that does some stuff (there's some mootools, but there are comments too):

// namespace.templatesCache is an object ( {} ) defined inside the main app js file

var 
    needXHR = false, // for callback function
    templateHTML = ""; //template html

if(!(templateHTML = namespace.templatesCache[template_name])){ //if template is not cached

    templateHTML = (this.helpers.supportLocalStorage) ? localStorage.getItem(template_name) : ""; //if browser supports local storage, check if I can retrieve it

    if(templateHTML === "" || templateHTML === null){ // if I don't have a template (usually, first time), retrieve it by ajax

      needXHR = true;  

      new Request.HTML({ //or jQuery's $.get( url /*, etc */ ) 

          url: namespace.URLS.BASE+"templates/_"+template_name+".tpl", // url of the template file

          onSuccess : function(t, e, html, js){

                namespace.templatesCache[template_name] = html; //cache it

                if(_this.helpers.supportLocalStorage){ //and store it inside local storage, if available
                  localStorage.setItem(template_name,html); 
                }

                //call callback      
          }
      }).get();

    }else{ //retrieved by localStorage, let's cache it

        namespace.templatesCache[template_name] = templateHTML;

    }

}

if(!needXHR){ // I retrieved template by cache/localstorage, not by Ajax

    //call callback    

}

and I call this helper in this way :

namespace.helpers.getTemplate('template_name', function( templateHTML ){
    // the callback function
});

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 :)

夜深人未静 2024-11-21 19:38:28

您可以尝试在 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 your script 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

回心转意 2024-11-21 19:38:28

当然,你所说的不会起作用,因为 liknek 元素的innerHTML 属性不会给你链接的内容。

您可以使用 Chevron 从链接加载外部模板,如下所示:

您在模板中添加链接到你的模板文件:

<link href="path/to/template.mustache" rel="template" id="templateName"/>

然后,在你的 JS 中,你可以像这样渲染你的模板:

$("#templateName").Chevron("render", {name: "Slim Shady"}, function(result){
    // do something with 'result'
    // 'result' will contain the result of rendering the template
    // (in this case 'result' will contain: My name is Slim Shady)
});

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:

<link href="path/to/template.mustache" rel="template" id="templateName"/>

Then, in you JS you can render your template like so:

$("#templateName").Chevron("render", {name: "Slim Shady"}, function(result){
    // do something with 'result'
    // 'result' will contain the result of rendering the template
    // (in this case 'result' will contain: My name is Slim Shady)
});

The docs of Chevron will give more examples

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文