更新我希望避免编译模板客户端,并让它们在我的本地ant构建过程中编译。可能类似于将jQuery和jQuery模板加载到犀牛中,依次传递$.template()函数每个.jst文件的内容,并构建一个"templates.js“,其中应该包含:
$.template['model-view'] = resultingFunction.toString();
// 1 for each .jst file这样,我可以在一个单独的文件中维护每个模板,并且避免让所有客户端冗余地编译相同的模板。
我正在使用jQuery模板,并希望将它们分离到自己的文件中(例如。use (模型-view.jst),在构建项目时编译成函数,并在jQuery .tmpl()范围中提供,供以后使用。
例如,给定文件model-view.jst
<li>${name}</li>这个文件和所有其他.jst文件都应该在构建时提取,编译成一个函数,以后可以在程序中的任何地方使用,如下所示:
$.tmpl('model-view', {
name: 'Matt'
});发布于 2011-07-10 18:21:33
我使用Node.js和coffeescript解决了这个问题,方法是将部分模板目录转换为可执行的、预编译的函数。希望这能有所帮助。
发布于 2011-06-24 08:08:36
我让你决定你是否喜欢它:)
在您的普通js库中,定义此函数:
function loadTemplate(templateName) {
$.ajax({
url: templateName + '.jst',
success: function(data) {
$.template(templateName, data);
}});
}然后,在master文件<head></head>部分中可以添加:
<script type="text/javascript">loadTemplate('model-view');</script>
<script type="text/javascript">loadTemplate('another-model-view');</script>这样您就可以在代码中的任何地方使用
$.tmpl('model-view', your-data)
$.tmpl('another-model-view', your-data)希望它能帮上忙
https://stackoverflow.com/questions/6464933
复制相似问题