隐藏模板代码

时间:2011-06-27 16:06:14

标签: javascript jquery-templates

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

1 个答案:

答案 0 :(得分:0)

$.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" );
}
相关问题