这几天接触Node.js + Express,实然发现无论使用jade还是ejs模板系统都会自动创建一个layout.(jade|ejs)文件。并且以文件名约定的方式使用,而 非在代码中指定Layout。但是在实际的项目中往往可能需要多个Layout文件来渲染页面板式,模板系统的创建者不可能没有这方面的考虑。那么应该如 何实现哪?
网上有很多相关的nodejs express ejs模板的代码.但很少关于ejs修改成html的配置.都是默认的为多.
现教大家如何配置将.ejs指向.html
这样以后修改起来非常方便.
/**
* Module dependencies.
*/
var express = require("express")
, routes = require("./routes")
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set("views", __dirname + "/views");
app.set("view engine", "html");
app.register(".html", {
compile: function(str, options){
return function(locals){
return str;
};
}
}); // 指向html模板
app.set("view options", {layout: false}); // 指定index不指定layout模板.
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + "/public"));
});
app.configure("development", function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure("production", function(){
app.use(express.errorHandler());
});
// Routes
app.get("/", routes.index);
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
loading