2013-02-16 37 views
7

Tôi không muốn sử dụng công cụ tô hoa Ngọc theo mặc định bằng Express. Tôi cố gắng để làm theo hướng dẫn này nhưng nó không thành công:Làm thế nào để sử dụng các mẫu gạch dưới thay vì Jade trong Express?

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

Các lỗi trong câu hỏi là:

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) 

tôi có được điều này khi tôi cố gắng khởi động server với:

node app.js 

Làm cách nào để giải quyết vấn đề này?

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

tuyến/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

Nhiều bối cảnh, xin vui lòng. Dấu vết ngăn xếp thường vô ích nếu không có mã để tham chiếu chúng. –

+0

@JackManey Đã cập nhật câu hỏi để bao gồm mã. Tôi khá nhiều chỉ cần làm theo hướng dẫn trên liên kết đó. – TK123

+0

Vì vậy, bạn có thể thử 'ejs',' jshtml' hoặc 'hogan.js'? –

Trả lời

5

Sử dụng consolidate.js để chuyển đổi chức năng mẫu gạch để chấp nhận các định dạng Express yêu cầu trong 3.x (path[, locals], callback).

2

Trước tiên, bạn đang gọi app.engine với tên tiện ích mở rộng và đối tượng trong khi hàm này lấy hàm làm tham số thứ hai (xem source documentation).

Chức năng này có 3 tham số: đường dẫn đến tệp, tùy chọn và gọi lại.

Như được viết trong documentation, bạn nên sử dụng consolidate.js làm người trợ giúp để sử dụng các công cụ tạo mẫu không thân thiện với người dùng.

Dưới đây là một tích hợp đơn giản của consolidate.js trích dẫn từ README của nó và thích nghi với sử dụng gạch dưới:

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

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

Ngoài ra, tôi không biết làm thế nào để xử lý layout.html của bạn với gạch dưới Express, tôi không nghĩ rằng nó có thể ra khỏi hộp.

2
var cons = require('consolidate'); 

// xem động cơ thiết lập

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

trong terminal

npm install consolidate --save 
Các vấn đề liên quan