Started ServerResponse#render()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent f6ca253 · Browse files · 6 files·+61 added−70 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+61 added−70 removed
- tests:+25 added−0 removed
- other:+36 added−70 removed
Provenance
- Commit
21f904d- Tree
a3a1f07- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
f6ca253
6 files changed+61 added−70 removed
modified:lib/express/index.js+7 added-1 removed
25 unmodified lines26272829303031323334353625 unmodified lines */
exports.createServer = function(){ return new Server(Array.prototype.slice.call(arguments));};No newline at end of file};
/** * View extensions. */
require('./view');No newline at end of filemodified:lib/express/server.js+8 added
23 unmodified lines24252627282930313233343536373839106 unmodified linesExpand all23 unmodified lines var self = this; this.config = {}; this.settings = {}; connect.Server.call(this, middleware || []);
// Expose app to extensions this.use('/', function(req, res, next){ req.app = res.app = self; next(); });
// Use router, expose as app.get(), etc this.use('/', router(function(app){ self.routes = app; }));};106 unmodified linesExpand allmodified:lib/express/view.js+21 added-69 removed
67 unmodified lines68 }69 };70 71 /**72- * Render _view_ with _options_.72+ * Render `view` with the given `options` and optional callback `fn`.73+ * When a callback function is given a response will _not_ be made74+ * automatically, however otherwise a response of _200_ and _text/html_ is given.75 *74- * Views are looked up relative to the'views' path setting. 75- * View filenames should conform to NAME.TYPE.ENGINE so for example76- * 'layout.html.ejs', 'ejs' represents the template engine, 'html'77- * represents the type of content being rendered, which is then passed78- * to contentType().79- *80- * Engines must export a render() method accepting the template string81- * and a hash of options. Engines can respond to the options listed below82- * as well as their own arbitrary ones. The "filename" option is always83- * passed as the path to the given _view_, allowing engines to perform84- * better error reporting.85- *86- * Options:87- *88- * - encoding: Passed to Request#respond()89- * - layout: The layout to use, none when falsey. Defaults to 'layout'90- * - locals: Most engines support a hash of local variable names / values.91- * - context: Most engines support an evaluation context (the 'this' keyword). 92- * Defaults to the current Request instance.93- *94- * Optionally you may also pass a _callback_ function which95- * will be called instead of responding with the 200 status code.96- *76 * @param {String} view77 * @param {Object} options78 * @param {Function} fn79 * @api public80 */81 103-http.ServerResponse.prototype.render = function(view, options){82+http.ServerResponse.prototype.render = function(view, options, fn){83 options = options || {};105- var type = options.partial ? 'partials' : 'views',106- path = set(type) + '/' + view,107- parts = view.split('.'),108- engine = parts[parts.length - 1],109- contentType = parts.slice(-2)[0],110- layout = options.layout === undefined ? 'layout' : options.layout;111-112- // Default filename and evaluation context113- options.filename = path;114- options.context = options.context || this;115-116- // Merge locals117- options.locals = options.locals || {};118- for (var key in helpers) {119- options.locals[key] = helpers[key];120- }121-122- // View contents123- var content = cache[type][path] || fs.readFileSync(path).toString(options.encoding || 'utf8');124- 125- // Render the view126- content = (engines[engine] = engines[engine] || require(engine)).render(content, options);127- 128- // Set Content-Type129- if (type === 'views' && !this.headers['Content-Type']) {130- this.headers['Content-Type'] = mime.type(contentType);131- }132- 133- // Layout support134- if (layout) {135- layout = layout.indexOf('.') > 0136- ? [layout, contentType, engine].join('.')137- : layout;138- // TODO: callback139- // TODO: deep merge options140- this.render(layout, {141- layout: false,142- locals: { body: content }84+ var self = this,85+ root = options.root || this.app.set('views') || process.cwd() + '/views',86+ path = root + '/' + view,87+ ext = extname(view),88+ layout = options.layout === true89+ ? root + '/layout' + ext90+ : options.layout !== false91+ ? root + '/' + layout92+ : false;93+ fs.readFile(path, 'utf8', function(err, str){94+ var engine = cache[ext] || (cache[ext] = require(ext.substr(1)));95+ str = engine.render(str, options);96+ self.writeHead(200, {97+ 'Content-Type': 'text/html',98+ 'Content-Length': str.length99 });144- } else if (type === 'partials') {145- return content;146- // TODO: callback147- } else {148- this.writeHead(200, content, options.encoding);149- }100+ self.end(str);101+ });102 };added:test/fixtures/index.jade+1 added
1+p Welcomeadded:test/fixtures/layout.jade+5 added
1+!!!2+html3+ head4+ title Express5+ body!= bodyadded:test/view.test.js+19 added
12345678910111213141516171819
/** * Module dependencies. */
var express = require('express');
module.exports = { 'test #render()': function(assert){ var app = express.createServer(); app.set('views', __dirname + '/fixtures'); app.get('/', function(req, res){ res.render('index.jade', { layout: false }); }); assert.response(app, { url: '/' }, { body: '<p>Welcome</p>' }); }};No newline at end of file