Added layout support
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 21f904d · Browse files · 4 files·+53 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+53 added−10 removed
- tests:+23 added−3 removed
- other:+30 added−7 removed
Provenance
- Commit
414acbc- Tree
c9006ec- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
21f904d
4 files changed+53 added−10 removed
modified:lib/express/view.js+30 added-7 removed
79 unmodified lines80818283848586878889909192939489909192939495959697989910010110210310410510610710810911011111211311411511611711811912012112212312412579 unmodified lines */
http.ServerResponse.prototype.render = function(view, options, fn){ options = options || {};
if (options.layout === undefined) { options.layout = true; }
// Defaults var self = this, root = options.root || this.app.set('views') || process.cwd() + '/views', path = root + '/' + view, ext = extname(view), layout = options.layout === true ? root + '/layout' + ext : options.layout !== false ? root + '/' + layout : false; fs.readFile(path, 'utf8', function(err, str){ var engine = cache[ext] || (cache[ext] = require(ext.substr(1))); str = engine.render(str, options); ? 'layout' + ext : options.layout;
// Send response function send(str) { self.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': str.length }); self.end(str); }
// Render fs.readFile(path, 'utf8', function(err, str){ // TODO: error handling var engine = cache[ext] || (cache[ext] = require(ext.substr(1))); str = engine.render(str, options); if (layout) { options.locals = options.locals || {}; options.layout = false; options.locals.body = str; self.render(layout, options, function(err, str){ send(str); }); } else if (fn) { fn(null, str); } else { send(str); } });};added:test/fixtures/cool.layout.jade+1 added
1+cool!= bodymodified:test/fixtures/layout.jade-3 removed
1-!!!1 html3- head4- title Express2 body!= bodymodified:test/view.test.js+22 added
13 unmodified lines1415161718192021222324252627282930313233343536373839404113 unmodified lines }); assert.response(app, { url: '/' }, { body: '<p>Welcome</p>' }); }, 'test #render() layout': function(assert){ var app = express.createServer(); app.set('views', __dirname + '/fixtures'); app.get('/', function(req, res){ res.render('index.jade'); }); assert.response(app, { url: '/' }, { body: '<html><body><p>Welcome</p></body></html>' }); }, 'test #render() specific layout': function(assert){ var app = express.createServer(); app.set('views', __dirname + '/fixtures'); app.get('/', function(req, res){ res.render('index.jade', { layout: 'cool.layout.jade' }); }); assert.response(app, { url: '/' }, { body: '<cool><p>Welcome</p></cool>' }); }};No newline at end of fileNo newline at end of file