简体   繁体   English

NodeJS / Express:使用Bliss作为模板引擎的错误

[英]NodeJS/Express: Errors using Bliss as the template engine

I'm trying to replace Jade with Bliss as the template engine for a sample Express web site on NodeJS. 我正在尝试用Bliss替换Jade作为NodeJS上示例Express网站的模板引擎。 Here's the contents of app.js: 这是app.js的内容:

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

I've tried a few things from other SO issues, but I'm not familiar enough with Node, Express or Bliss to debug why it's returning the following 500 error: 我已经从其他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'

I'm fairly confident that the templates and views are set up properly, as they are very simple and follow the Bliss wiki closely. 我非常有信心模板和视图设置正确,因为它们非常简单并且非常遵循Bliss wiki。

Is this due to Bliss not being fully compatible with Express? 这是由于Bliss与Express不完全兼容吗? Is there a better way to set it up to work properly? 有没有更好的方法来设置它正常工作?

Here's how I solved the problem 这就是我解决问题的方法

assumes all my bliss files have .js.html extension 假设我的所有幸福文件都有.js.html扩展名

FILES FILES

app.js 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) 
});

router file index.js 路由器文件index.js

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 file 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)

layout1.js.html 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>

layout2.js.html is the same except has LAYOUT 2 as its text layout2.js.html是相同的,除了LAYOUT 2作为其文本

Now play with app.js/app.engine function to watch how you can affect the context at different stages 现在使用app.js / app.engine函数来观察如何在不同阶段影响上下文

COMMENT: now that I got express.js and bliss working, I decided to go ahead and use jade. 评论:现在我得到了express.js和bliss的工作,我决定继续使用玉器。 I like bliss' syntax but in addition to spending way too much time getting bliss to work, I decided I wasn't ready to commit fully to it :) 我喜欢bliss'的语法,但除了花太多时间让幸福工作之外,我觉得我还没准备好完全承诺:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM