快速渲染函数具有回调返回字符串而不是HTML

时间:2014-06-27 22:19:34

标签: node.js express

我是带有快速框架的Nose.JS的游戏用户,我尝试使用此层次结构为HTML做引擎:

<template>
  <layout>
    <view>
  </layout>
</template>

所以,这是我的server.js文件(croped):

var express = require('express')
        , http = require('http');
var app = module.exports = express();

app.configure(function() {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.engine('html', require('ejs').renderFile);

    app.use(express.favicon());
    app.use(express.static(__dirname + '/public'));
});

// Routes 
app.get('/', function(req, res) {

    var data = { /* Some data here */ };

    /* Probleme start here :
     * I want render the view HTML, 
     * then render the layout HTML with the view content,
     * then render the template to display
     */

    res.render('view', data, function(err, view) {
        data.view = view;
        res.render('layout', data, function(err, layout) {
            data.layout = layout;
            res.render('template', data);                
        });           
    });
});

就像我在代码上说的那样: 我想渲染视图HTML,然后使用视图内容呈现布局HTML,然后渲染要显示的模板。但是 data.view data.layout 是字符串而不是HTML(例如:HEADER<br/>Menu<br/>),所以如果我将HTML代码放在这些文件上,我就会得到HTML最后一页上的代码..

----- [编辑:添加.ejs文件] -----

template.ejs

<div><%= layout %></div>

layout.ejs

HEADER<br/>
Menu<br/>
<%= view %>
FOOTER

view.ejs

<p>View</p>

如何在 data.view data.layout 上显示HTML结果?!

谢谢大家,对不起我的英文

亚瑟

1 个答案:

答案 0 :(得分:1)

FWIW这是我用来在Express 4中获得布局支持(用ejs测试):

/*
   Usage:
     Set a global/default layout with:
        app.set('view layout', 'bar');
     Set a layout per-render (overrides global layout) with:
        res.render('foo', { layout: 'bar' });
     Or disable a layout if a global layout is set with:
        res.render('foo', { layout: false });
     If no layout is provided using either of the above methods,
     then the view will be rendered as-is like normal.

     Inside your layout, the variable `body` holds the rendered partial/child view.

   Installation:
     Call `mpResponse();` before doing `require('express');` in your application.
*/

function mpResponse() {
  var expressResponse = require('express/lib/response'),
      expressResRender = expressResponse.render;
  expressResponse.render = function(view, options, fn) {
    options = options || {};
    var self = this,
        req = this.req,
        app = req.app,
        layout,
        cb;

    // support callback function as second arg
    if (typeof options === 'function')
      fn = options, options = {};

    // merge res.locals
    options._locals = self.locals;

    // default callback to respond
    fn = fn || function(err, str) {
      if (err) return req.next(err);
      self.send(str);
    };

    if (typeof options.layout === 'string')
      layout = options.layout;
    else if (options.layout !== false
             && typeof app.get('view layout') === 'string')
      layout = app.get('view layout');

    if (layout) {
      cb = function(err, str) {
        if (err) return req.next(err);
        options.body = str;
        expressResRender.call(self, layout, options, fn);
      };
    } else
      cb = fn;

    // render
    app.render(view, options, cb);
  };
}