node.js - Express render function with callback return string and not HTML -


i'm nooby user of nose.js express framework , try engine html hierarchy :

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

so, server.js file (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 = { /* data here */ };      /* probleme start here :      * want render view html,       * render layout html view content,      * render template 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);                         });                }); }); 

like on code : want render view html, render layout html view content, render template display. data.view , data.layout string , not html( : header<br/>menu<br/>) , if put html code on files have html code on final page..

-----[edit : add .ejs files]-----

template.ejs

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

layout.ejs

header<br/> menu<br/> <%= view %> footer 

view.ejs

<p>view</p> 

how can result of html display on data.view , data.layout ?!

thank , sorry english

arthur

fwiw i've used layout support in express 4 (tested ejs):

/*    usage:      set global/default layout with:         app.set('view layout', 'bar');      set layout per-render (overrides global layout) with:         res.render('foo', { layout: 'bar' });      or disable layout if global layout set with:         res.render('foo', { layout: false });      if no layout provided using either of above methods,      view rendered as-is normal.       inside layout, variable `body` holds rendered partial/child view.     installation:      call `mpresponse();` before doing `require('express');` in 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 second arg     if (typeof options === 'function')       fn = options, options = {};      // merge res.locals     options._locals = self.locals;      // default callback 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);   }; } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -