refactored res.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 0b1378a · Browse files · 2 files·+38 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+38 added−32 removed
- other:+38 added−32 removed
Provenance
- Commit
2ff991b- Tree
c480719- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
0b1378a
2 files changed+38 added−32 removed
modified:examples/jade/app.js+5 added-5 removed
13 unmodified lines141516171819202118192021222324252622 unmodified lines49505152525354555621 unmodified lines13 unmodified lines
// Auto-compile sass to css with "compiler"// and then serve with connect's staticProvider
var app = express.createServer( express.compiler({ src: pub, enable: ['sass'] }) , express.static(pub));var app = express.createServer();app.use(express.compiler({ src: pub, enable: ['sass'] }));app.use(app.router);app.use(express.static(pub));app.use(express.errorHandler({ dump: true, stack: true }));
// Optional since express defaults to CWD/views
app.set('views', __dirname + '/views');22 unmodified linesapp.get('/users/callback', function(req, res){ // a callback is also accepted res.partial('users/user', users, function(err, html){ if (err) throw err; console.log(html); res.send(html); });});
21 unmodified linesmodified:lib/view.js+33 added-27 removed
105 unmodified lines106 } else {107 options.scope = object;108 }109 }110- return res.render(view, options, null, parent);110+ return res.render(view, options, null, parent, true);111 }112 113 // Collection support114 if (collection) {92 unmodified lines207 * @param {Function} fn208 * @api public209 */210 211-res.render = function(view, opts, fn, parent){211+res.render = function(view, opts, fn, parent, sub){212 // support callback function as second arg213- if (typeof opts === 'function') {213+ if ('function' == typeof opts) {214 fn = opts, opts = null;215 }216- 216+217 var options = {}218 , self = this219 , app = this.app220 , helpers = app.viewHelpers90 unmodified lines311 312 // Provide filename to engine313 options.filename = view.path;314 315- function error(err) {316- if (fn) {317- fn(err);318- } else {319- self.req.next(err);320- }321- }322-315 // Attempt render316 try {317 var engine = view.templateEngine318 , template = cacheTemplates319 ? cache[view.path] || (cache[view.path] = engine.compile(view.contents, options))320 : engine.compile(view.contents, options)321 , str = template.call(options.scope, options);322+323+ // layout expected324+ if (layout) {325+ options.isLayout = true;326+ options.layout = false;327+ options.body = str;328+ this.render(layout, options, null, view, true);329+ // partial return330+ } else if (partial) {331+ return str;332+ // render complete, and 333+ // callback given334+ } else if (fn) {335+ fn(null, str);336+ // respond337+ } else {338+ this.send(str);339+ }340+341 } catch (err) {331- return error(err);342+ // callback given343+ if (fn) {344+ fn(err);345+ // unwind to root call346+ } else if (sub) {347+ throw err;348+ // root template, next(err)349+ } else {350+ this.req.next(err);351+ }352 }333-334- // Layout support335- if (layout) {336- options.isLayout = true;337- options.layout = false;338- options.body = str;339- self.render(layout, options, fn, view);340- } else if (partial) {341- return str;342- } else if (fn) {343- fn(null, str);344- } else {345- this.send(str);346- }353 };354 355 /**356 * Hint at view path resolution, outputting the15 unmodified lines