15

我想缓存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);

这不会缓存模板。我能弄清楚的唯一方法是使用链接标签,但是如何在javascript没有ajax请求的情况下调用模板内容?

这行不通(当然)...

HTML

<link type="text/html" href="/mustache/template.tpl" id="mustache-template" />

Javascript

document.getElementById('mustache-template').innerHTML;
4

3 回答 3

8

这个问题很有趣!几个月前,当我开始在 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
});

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

我希望它会有所帮助,我很乐意收到有关这些东西的反馈/建议 :)

于 2011-06-09T23:47:58.653 回答
1

您可以尝试将模板加载iframe到包含 HTML 页面(将被缓存)中,其中包含所有script标签。

然后您可以从主页读取它们,或者将它们从 iframe 推送到parent窗口。

这就是我在使用pure.js模板时所做的

于 2011-06-09T14:32:34.920 回答
1

你说的当然不行,因为 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 的文档将提供更多示例

于 2012-08-05T03:37:14.797 回答