Skip to content
Myr

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

2 files+38 added−32 removed

modified:examples/jade/app.js+5 added-5 removed
modified:lib/view.js+33 added-27 removed
105 unmodified lines
106 } 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 }112113 // Collection support114 if (collection) {
92 unmodified lines
207 * @param {Function} fn208 * @api public209 */210211res.render = function(view, opts, fn, parent){211res.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 216217 var options = {}218 , self = this219 , app = this.app220 , helpers = app.viewHelpers
90 unmodified lines
311312 // Provide filename to engine313 options.filename = view.path;314315 function error(err) {316 if (fn) {317 fn(err);318 } else {319 self.req.next(err);320 }321 }322315 // 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);322323 // 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 }340341 } 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 }333334 // 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};354355/**356 * Hint at view path resolution, outputting the
15 unmodified lines