Node.js Express如何将Underscore.js注册为视图引擎的示例?

8

Underscore.js 没有像 EJS 和 Jade 那样的编译函数,但可以作为 Node.js 模块使用。请问有人能提供在 Express 应用中如何使用它的示例吗?

2个回答

20
var _ = require('underscore');

app.register('.html', {
  compile: function (str, options) {
    var template = _.template(str);
    return function (locals) {
      return template(locals);
    };
  }
});

注意:此为 Express <3 版本的内容。 - UpTheCreek

4
现在使用 Express 3.0 有所不同。简单的解决方案是: https://github.com/haraldrudell/uinexpress
npm install uinexpress

那么

app.configure(function () {
app.engine('html', require('uinexpress').__express)
app.set('view engine', 'html')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接