Javascript NodeJS/Express:使用Bliss作为模板引擎时出错
我正在尝试将Jade替换为NodeJS上一个示例Express网站的模板引擎。以下是app.js的内容:Javascript NodeJS/Express:使用Bliss作为模板引擎时出错,javascript,node.js,templates,express,Javascript,Node.js,Templates,Express,我正在尝试将Jade替换为NodeJS上一个示例Express网站的模板引擎。以下是app.js的内容: var express = require('express'), routes = require('./routes'), http = require('http'), path = require('path'), Bliss = new require('bliss'), bliss = new Bliss({ext: '.jhtml'}),
var express = require('express'),
routes = require('./routes'),
http = require('http'),
path = require('path'),
Bliss = new require('bliss'),
bliss = new Bliss({ext: '.jhtml'}),
app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
// app.set('view engine', 'bliss'); /* replaced with app.engine() call below */
app.use(express.favicon(__dirname + '/public/img/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public'))); });
app.configure('development', function(){
app.use(express.errorHandler());
app.locals.pretty = true; });
app.get('/', /* routes.index */ function(req, res){
var index = bliss.compile('index');
bliss.render(index); });
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port')); });
我尝试了其他SO问题中的一些方法,但我对Node、Express或Bliss不够熟悉,无法调试它返回以下500个错误的原因:
500 TypeError: Object function anonymous() { var __out=[],write=__out.push.bind(__out),__tmp=0,render=this.render;write("index"); return __out.join(''); } has no method 'indexOf'
我非常确信模板和视图的设置是正确的,因为它们非常简单,并且密切遵循Bliss wiki
这是因为极乐与Express不完全兼容吗?
有没有更好的方法让它正常工作?以下是我解决问题的方法 假设我所有的bliss文件都有.js.html扩展名 文件夹 app.js
var Bliss = require('bliss');
var bliss = new Bliss();
app.set('views', __dirname + '/views/bliss');
app.set('view engine', 'js.html');
app.engine('js.html', function(path, options, fn){
// res.render is calling this
// update context here (available to all templates)
var ctx = options.context;
console.log("context", ctx);
if (typeof ctx === 'undefined') {
ctx = {};
}
// these will write over what's already in the context
ctx.name2 = 'MEH2';
//ctx.name1 = 'MEH1';
//ctx.layout = 'layout2';
var output = bliss.render(path, ctx);
fn(null, output); // fn ==> function(error, str)
});
exports.index = function(req, res){
console.log("ready to render index1.js.html");
var ctx = {context : {name1 : 'mike1'}};
ctx.context.layout = 'layout1';
res.render('index1', ctx);
};
路由器文件index.js
var Bliss = require('bliss');
var bliss = new Bliss();
app.set('views', __dirname + '/views/bliss');
app.set('view engine', 'js.html');
app.engine('js.html', function(path, options, fn){
// res.render is calling this
// update context here (available to all templates)
var ctx = options.context;
console.log("context", ctx);
if (typeof ctx === 'undefined') {
ctx = {};
}
// these will write over what's already in the context
ctx.name2 = 'MEH2';
//ctx.name1 = 'MEH1';
//ctx.layout = 'layout2';
var output = bliss.render(path, ctx);
fn(null, output); // fn ==> function(error, str)
});
exports.index = function(req, res){
console.log("ready to render index1.js.html");
var ctx = {context : {name1 : 'mike1'}};
ctx.context.layout = 'layout1';
res.render('index1', ctx);
};
index1.js.html文件
@!(ctx)
@function title() {
WOW @ctx.name1, @ctx.name2
}
@function body() {
<div>
<h2> Hello @ctx.name1</h2>
</div>
}
@*choose the layout based on the context*@
@render(ctx.layout, body, title, ctx)
@!(ctx)
@函数名(){
哇@ctx.name1,@ctx.name2
}
@功能体(){
你好@ctx.name1
}
@*根据上下文选择布局*@
@渲染(ctx.layout、正文、标题、ctx)
layout1.js.html
@!(body, title, ctx)
<!DOCTYPE html>
<html>
<head>
<title> @title() </title>
</head>
<body>
<h1> REALLY @ctx.title </h1>
LAYOUT 1
@body()
</body>
</html>
@!(正文、标题、ctx)
@标题()
真的吗@ctx.title
布局1
@正文()
layout2.js.html与之相同,只是其文本为layout2
现在使用app.js/app.engine函数来观察在不同阶段如何影响上下文
评论:现在我已经让express.js和bliss开始工作,我决定继续使用jade。我喜欢bliss的语法,但除了花太多时间让bliss发挥作用之外,我还决定我还没有准备好完全投入其中:)是的,我当然看到了。我正在使用的应用程序示例首先引发了我的问题,但它不起作用。我希望熟悉将Bliss与Express结合使用的人能够看到这个问题。