Skip to content

Instantly share code, notes, and snippets.

@totherik
Last active August 29, 2015 13:55
Show Gist options
  • Save totherik/8699430 to your computer and use it in GitHub Desktop.
Save totherik/8699430 to your computer and use it in GitHub Desktop.
var express = require('express'),
engine = require('view-engine');
var renderer, app;
renderer = engine.create('dust', require('dustjs-linkedin'));
app = express();
app.set('view-engine', renderer);
module.exports = function middleware(req, res) {
var engine, context;
engine = req.app.get('view-engine');
context = engine.createContext(res);
context.add({ name: 'Frank' });
engine.render('my-template', context).pipe(res);
// possibly redundant. use `render` for both streaming and non-streaming?
// engine.stream('./my-templates', context).pipe(res);
// engine.json(context).pipe(res);
// engine.jsonp(context).pipe(res);
// engine.binary('').pipe(res);
// engine.send('').pipe(res); // auto-negotiate mime-type
}
'use strict';
exports.create = function (ext, renderer) {
return {
createContext: function (base) {
// special-case express for app & res locals?
var context = base || {};
return {
add: function (data) {
util.merge(context, data);
return this;
}
};
},
render: function (name, context) {
// create renderer stream wrapper
return renderer.stream(name, context);
},
json: function (context) {
return {
pipe: function () {
}
}
},
jsonp: function (context) {
},
binary: function (buffer) {
},
send: function (obj) {
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment