如何在Express中使用Underscore模板代替Jade?

7

我不想使用Express默认提供的Jade模板引擎。我尝试按照这个指南操作,但失败了:

http://blog.luksidadi.com/expressjs-underscore-template/

问题中的错误是:
node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: callback function required
    at Function.engine (/home/me/blog/node_modules/express/lib/application.js:173:38)
    at Object.<anonymous> (/home/tk/blog/app.js:28:5)
    at Module._compile (module.js:432:26)
    at Object..js (module.js:450:10)
    at Module.load (module.js:351:31)
    at Function._load (module.js:310:12)
    at Array.0 (module.js:470:10)
    at EventEmitter._tickCallback (node.js:192:40)

当我尝试使用以下命令启动服务器时,出现了这个错误:

node app.js

如何解决这个问题?

app.js:

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  //app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

// Add these lines to register underscore template
var _ = require('underscore');
app.engine('.html', {
  compile: function(str, options){
    var compiled = require('underscore').template(str);
    return function(locals) {
        return compiled(locals);
    };
  }
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

routes/index.js:

/*
 * GET home page.
 */

exports.index = function(req, res){
  res.render('index.html', { title: 'Express' });
};

layout.html:

< html >
  < head >
    < title ><%=title%>< /title >
  < /head >
  < body >
  <%=body%>
  < /body >
< /html >

index.html:

Hello world

请提供更多上下文。堆栈跟踪通常没有参考代码是无用的。 - user554546
@JackManey 更新了问题并附上了代码。我基本上只是按照那个链接上的指南操作。 - user967451
那么你可以尝试使用 ejsjshtmlhogan.js - Ron van der Heijden
3个回答

5

使用 consolidate.js 将 Underscore 的模板函数转换为 3.x 版本 Express 所需的格式(path [,locals],callback)


2
var cons = require('consolidate');

//视图引擎设置

app.engine('html',cons.underscore);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');

在终端中
npm install consolidate --save 

2
首先,你正在使用一个扩展名和一个对象来调用app.engine,而它需要一个函数作为第二个参数(请参见源文档)。
该函数有3个参数:文件路径、选项和回调函数。
正如文档中所写,建议使用consolidate.js作为帮助程序,以使用不支持 express 的模板引擎。
下面是一个简单的consolidate.js集成示例,摘自其README并适配使用 underscore:
// assign the swig engine to .html files
app.engine('html', cons.underscore);

// set .html as the default extension 
app.set('view engine', 'html');

此外,我不知道如何在Express中使用下划线处理您的layout.html文件,我认为这不是开箱即用的。

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