node.js - 服务客户端 Jade 模板

标签 node.js backbone.js express pug marionette

我想在客户端使用 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/

相关文章:

javascript - 具有条件字段的 Backbone 形式

javascript - 如何编写带有 M/D/YYYY 格式日期条件并存储为字符串的查找查询

javascript - Node 编码和解码 utf-16 缓冲区

javascript - BackboneJS 重置模型

javascript - 在 Backbone.js 中按子字符串过滤集合

node.js - 使用查询字符串表达 js 路由

javascript - Express 中来自 React 子组件的 POST 请求

javascript - Express.js 查看 "globals"

javascript - 我可以使用一些脚本来使我的 mongoose 数据库部署更加简化吗?

javascript - 重新加载页面或浏览器后如何处理 Kaltura session 连接