我想在客户端使用 Backbone 的 Jade 模板。我怎样才能做到这一点?
目前,我已成功配置 Backbone (Marionette) 来编译 Jade 模板以在其 View 中使用:
Marionette.TemplateCache.prototype.compileTemplate = (tmplStr) ->
console.log "jade stuff: ", jade.compile(tmplStr)
return jade.compile(tmplStr)
“问题”是:我目前正在编写如下模板:
script(type="text/template", id="tmplMainView")
| h1= title
| p= content
注意管道 (|
) 是为了防止 Jade 在服务器端尝试解释/解析它们。我怎样才能消除那些?
更新
也许我可以使用 jade --client
标志...但它提供了一个编译函数:例如
h1= title
成为
function anonymous(locals, attrs, escape, rethrow, merge) {
attrs = attrs || jade.attrs; escape = escape || jade.escape; rethrow = rethrow || jade.rethrow; merge = merge || jade.merge;
var buf = [];
with (locals || {}) {
var interp;
buf.push('<h1>');
var __val__ = title
buf.push(escape(null == __val__ ? "" : __val__));
buf.push('</h1>');
}
return buf.join("");
}
这意味着我必须为每个模板准备 1 个 Jade/已编译的 JS?我该如何使用它?另外我认为许多 JS 文件是一种缓慢的工作方式?但是由于模板函数都是匿名命名的,我该如何连接或以某种方式有效地使用它们呢?
最佳答案
检查 ClientJade项目。
来自他们的网站:
clientjade is a command line tool to compile your jade templates into client side templates for use in the browser. It will automatically include everything you need to render the templates, no need to include jade.js or runtime.js.
$ clientjade test1.jade test2.jade > templates.js
And then include template.js file in your html. To render the templates, just make a call like this:
//jade.render(domNode, templateName, data);
jade.render(document.getElementById('test1'), 'test1', { name: 'Bob' });
关于node.js - 服务客户端 Jade 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13813789/