【问题标题】:Hide the template code隐藏模板代码
【发布时间】:2011-06-27 16:06:13
【问题描述】:

我正在开发一个小的 javascript 库,它使用可爱的 jquery-tmpl 引擎。我想避免强迫用户将必要的模板粘贴到他/她的 html 中(或者完全面对模板代码)。
所以我的问题是:有没有办法隐藏我的模板代码,最好是在我的 js 文件中,据我所知,脚本标签是必要的(如果只是为了能够用 jquery 选择它)。
到目前为止,我唯一发现的是将模板放在外部文件中,例如 here,但这并不是我真正想要的。
谢谢

【问题讨论】:

    标签: javascript jquery-templates


    【解决方案1】:
    $.template('yourTemplateName', 'yourTemplateCode');
    

    来自http://api.jquery.com/jQuery.template/

    该方法将模板参数中的标记编译为命名模板,可以使用name参数中指定的字符串进行引用。

    返回值是编译后的模板函数。

    示例:创建与名称“summaryTemplate”关联的已编译模板,然后通过名称引用它进行渲染:

    // Convert the markup string into a named template
    $.template( "summaryTemplate", "<li>${Name}</li>" );
    
    function renderList() {
        // Render the movies data using the named template: "summaryTemplate"
        $.tmpl( "summaryTemplate", movies ).appendTo( "#moviesList" );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-28
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多