2011-06-27 83 views
0

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

回答

0
$.template('yourTemplateName', 'yourTemplateCode'); 

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

这种方法编译模板参数作为命名模板,可以使用在名称参数指定的字符串中引用的标记。

返回值是编译模板函数。

举例:创建一个名为“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"); 
} 
+1

喔感谢....我想我应该考虑RTFM .. 。 – Jan