【问题标题】:How to cache mustache templates?如何缓存胡子模板?
【发布时间】:2011-06-09 12:12:18
【问题描述】:

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

【问题讨论】:

    标签: javascript templates caching external mustache


    【解决方案1】:

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

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


    Mustache 模板位于公共文件夹中:

    /public/templates/_template_name.tpl
    

    每当我需要模板时,我都有这个帮助器 getTemplate 可以做一些事情(有一些 mootools,但也有 cmets):

    // 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
    });
    

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 谢谢!很好的解决方法,但这对我来说似乎有点 hacky。欢迎所有其他建议。
      • 我围绕这个问题做了很多圈子。一旦你度过了创伤:iframe 是你不应该使用的东西,一切都很好。但我期待您能得到任何其他答案。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-14
        • 1970-01-01
        • 2012-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多