2013-02-16 28 views
7

私はExpressでデフォルトで提供されるJadeテンプレートエンジンを使いたくありません。私は、このガイドに従うことを試みたが、それは失敗します。Jade in Expressの代わりにアンダースコアテンプレートを使用するには?

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

問題のエラーは次のとおりです。

node app.js 

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) 

私は、サーバーを起動しようとすると、私はこれを取得します

これを解決するには?

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')); 
}); 

ルート/ 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 
+0

より文脈で

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

、してください。スタックトレースは、それらを参照するコードなしでは通常役に立たない。 –

+0

@JackManeyコードを含めるように質問が更新されました。私はちょうどそのリンクのガイドに従った。 – TK123

+0

'ejs'、' jshtml'または 'hogan.js'を試すことができますか? –

答えて

5

使用consolidate.js Expressは3.xの(path[, locals], callback)に必要とフォーマットを受け入れるように下線のテンプレート関数を変換します。

2

最初に、拡張名とオブジェクトを持つapp.engineを呼び出していますが、第2のパラメータ(source documentationを参照)として機能します。

この関数には、ファイルへのパス、オプション、およびコールバックの3つのパラメータがあります。

documentationで書かれているように、フレンドリーではないテンプレートエンジンを使用するには、consolidate.jsをヘルパーとして使用することをお勧めします。ここで

そのREADMEから引用consolidate.jsの簡単な統合とアンダースコアを使用するように適合:

// assign the swig engine to .html files 
app.engine('html', cons.underscore); 

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

また、私はエクスプレスの下にアンダースコアであなたのlayout.htmlを処理する方法がわからない、私はしないでくださいそれは箱の外にある可能性があると思います。

2
var cons = require('consolidate'); 

//ビューエンジンのセットアップターミナル

npm install consolidate --save 
関連する問題