【问题标题】:How to use Underscore templates instead of Jade in Express?如何在 Express 中使用 Underscore 模板而不是 Jade?
【发布时间】:2013-02-01 03:28:42
【问题描述】:

我不想使用 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

【问题讨论】:

  • 请提供更多上下文。堆栈跟踪通常没有引用它们的代码是无用的。
  • @JackManey 更新了问题以包含代码。我几乎只是按照该链接上的指南进行操作。
  • 所以你可以试试ejs, jshtml or hogan.js?

标签: javascript node.js express underscore.js pug


【解决方案1】:

使用consolidate.js 将Underscore 的模板函数转换为接受Express 在3.x 中要求的格式(path[, locals], callback)。

【讨论】:

    【解决方案2】:

    首先,您使用扩展名和对象调用app.engine,而它需要一个函数作为第二个参数(请参阅source documentation)。

    此函数有 3 个参数:文件路径、选项和回调。

    正如documentation 中所写,建议使用consolidate.js 作为辅助工具,以使用对表达不友好的模板引擎。

    这里是 consolidate.js 的简单集成,引用自其 README 并适用于使用下划线:

    // 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,我认为不可能开箱即用。

    【讨论】:

      【解决方案3】:
      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 
      

      【讨论】:

        猜你喜欢
        • 2012-07-14
        • 2015-04-23
        • 1970-01-01
        • 2012-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多