view.js
This file
Path
lib/view.js
303 lines · 7.1 KiB · JavaScript
Last changed by
do not try ../index when rendering a layoutTj Holowaychukbefore adoption65c8a95
History of this fileTake it
moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 1–18why, one step back12/*!3 * Express - view4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var path = require('path')13 , extname = path.extname14 , dirname = path.dirname15 , basename = path.basename16 , utils = require('connect').utils17 , View = require('./view/view')18 , Partial = require('./view/partial')Added union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758
line 19why, one step back19 , union = require('./utils').unionmoved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
line 20why, one step back20 , merge = utils.mergeAdded res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 21–22why, one step back21 , http = require('http')22 , res = http.ServerResponse.prototype;moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 23–45why, one step back2324/**25 * Memory cache.26 *27 * @type Object28 */2930var cache = {};3132/**33 * Expose constructors.34 */3536exports = module.exports = View;37exports.Partial = Partial;3839/**40 * Export template engine registrar.41 */4243exports.register = View.register;4445/**Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 46why, one step back46 * Partial render helper.moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
line 47why, one step back47 *Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 48why, one step back48 * @api privatemoved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 49–50why, one step back49 */50Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 51why, one step back51function renderPartial(res, view, options, locals, parent){moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 52–93why, one step back52 // Inherit parent view extension when not present53 if (parent && !~view.indexOf('.')) {54 view += parent.extension;55 }5657 // Allow collection to be passed as second param58 if (options) {59 if ('length' in options) {60 options = { collection: options };61 } else if (!options.collection && !options.locals && !options.object) {62 options = { object: options };63 }64 } else {65 options = {};66 }6768 // Inherit locals from parent69 merge(options, locals);70 71 // Merge locals72 if (options.locals) {73 merge(options, options.locals);74 }7576 // Partials dont need layouts77 options.renderPartial = true;78 options.layout = false;7980 // Deduce name from view path81 var name = options.as || Partial.resolveObjectName(view);8283 // Render partial84 function render(){85 if (options.object) {86 if ('string' == typeof name) {87 options[name] = options.object;88 } else if (name === global) {89 merge(options, options.object);90 } else {91 options.scope = options.object;92 }93 }Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 94why, one step back94 return res.render(view, options, null, parent);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 95–118why, one step back95 }9697 // Collection support98 var collection = options.collection;99 if (collection) {100 var len = collection.length101 , buf = '';102 delete options.collection;103 options.collectionLength = len;104 for (var i = 0; i < len; ++i) {105 var val = collection[i];106 options.firstInCollection = i === 0;107 options.indexInCollection = i;108 options.lastInCollection = i === len - 1;109 options.object = val;110 buf += render();111 }112 return buf;113 } else {114 return render();115 }116};117118/**Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 119–152why, one step back119 * Render `view` partial with the given `options`.120 *121 * Options:122 *123 * - `object` Single object with name derived from the view (unless `as` is present) 124 *125 * - `as` Variable name for each `collection` value, defaults to the view name.126 * * as: 'something' will add the `something` local variable127 * * as: this will use the collection value as the template context128 * * as: global will merge the collection value's properties with `locals`129 *130 * - `collection` Array of objects, the name is derived from the view name itself. 131 * For example _video.html_ will have a object _video_ available to it.132 *133 * @param {String} view134 * @param {Object|Array} options, collection, or object135 * @return {String}136 * @api public137 */138139res.partial = function(view, options){140 var app = this.app141 , options = options || {}142 , parent = {};143144 // root "views" option145 parent.dirname = app.set('views') || process.cwd() + '/views';146147 // utilize "view engine" option148 if (app.set('view engine')) {149 parent.extension = '.' + app.set('view engine');150 }151152 var str = renderPartial(this, view, options, null, parent);Removed partial() / render() status and header optionsTj Holowaychukbefore adoptionaaa8415
line 153why, one step back153 this.send(str);Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 154–156why, one step back154};155156/**moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 157–171why, one step back157 * Render `view` with the given `options` and optional callback `fn`.158 * When a callback function is given a response will _not_ be made159 * automatically, however otherwise a response of _200_ and _text/html_ is given.160 *161 * Options:162 * 163 * - `scope` Template evaluation context (the value of `this`)164 * - `debug` Output debugging information165 *166 * @param {String} view167 * @param {Object|Function} options or callback function168 * @param {Function} fn169 * @api public170 */171Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 172why, one step back172res.render = function(view, opts, fn, parent){Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
lines 173–175why, one step back173 // support callback function as second arg174 if (typeof opts === 'function') {175 fn = opts, opts = null;moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 176–177why, one step back176 }177 Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
lines 178–179why, one step back178 var options = {}179 , self = thismoved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 180–182why, one step back180 , app = this.app181 , helpers = app.viewHelpers182 , dynamicHelpers = app.dynamicViewHelpersMerge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
lines 183–184why, one step back183 , viewOptions = app.set('view options')184 , cacheTemplates = app.set('cache views');moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
line 185why, one step back185Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 186why, one step back186 // merge "view options"misc refactoringTj Holowaychukbefore adoption530328a
line 187why, one step back187 if (viewOptions) merge(options, viewOptions);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
line 188why, one step back188Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 189why, one step back189 // merge res.localsmisc refactoringTj Holowaychukbefore adoption530328a
line 190why, one step back190 if (this.locals) merge(options, this.locals);Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
lines 191–192why, one step back191192 // merge render() optionsmisc refactoringTj Holowaychukbefore adoption530328a
line 193why, one step back193 if (opts) merge(options, opts);Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 194why, one step back194moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 195–196why, one step back195 // Defaults196 var self = thisutilizing app.set()Tj Holowaychukbefore adoption334604d
line 197why, one step back197 , root = app.set('views') || process.cwd() + '/views'moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 198–205why, one step back198 , partial = options.renderPartial199 , layout = options.layout;200201 // Layout support202 if (true === layout || undefined === layout) {203 layout = 'layout';204 }205Changed default view scope to a plain objectTj Holowaychukbefore adoptiona49e1f4
lines 206–207why, one step back206 // Default execution scope to a plain object207 options.scope = options.scope || {};moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 208–215why, one step back208209 // Populate view210 options.parentView = parent;211212 // "views" setting213 options.root = root;214215 // "view engine" settingutilizing app.set()Tj Holowaychukbefore adoption334604d
line 216why, one step back216 options.defaultEngine = app.set('view engine');moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 217–218why, one step back217218 // Populate viewAdded View#upIndexPathTj Holowaychukbefore adoption32be4a7
line 219why, one step back219 // TODO: move this logic into the protosmoved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 220–235why, one step back220 var orig = view = partial221 ? new Partial(view, options)222 : new View(view, options);223224 // Ensure view exists, otherwise try _ prefix225 if (!view.exists) {226 view = partial227 ? new Partial(view.prefixPath, options)228 : new View(view.prefixPath, options);229 }230 231 // Ensure view _ prefix exists, otherwise try index232 if (!view.exists) {233 view = partial234 ? new Partial(orig.indexPath, options)235 : new View(orig.indexPath, options);Added View#upIndexPathTj Holowaychukbefore adoption32be4a7
lines 236–237why, one step back236 }237fixing testsTj Holowaychukbefore adoption2b907e9
line 238why, one step back238 // Ensure view exists, or try ../indexdo not try ../index when rendering a layoutTj Holowaychukbefore adoption65c8a95
line 239why, one step back239 if (!view.exists && !options.isLayout) {fixing testsTj Holowaychukbefore adoption2b907e9
lines 240–244why, one step back240 view = partial241 ? new Partial(orig.upIndexPath, options)242 : new View(orig.upIndexPath, options);243 }244moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 245–262why, one step back245 // Dynamic helper support246 if (false !== options.dynamicHelpers) {247 // cache248 if (!this.__dynamicHelpers) {249 this.__dynamicHelpers = {};250 for (var key in dynamicHelpers) {251 this.__dynamicHelpers[key] = dynamicHelpers[key].call(252 this.app253 , this.req254 , this);255 }256 }257258 // apply259 merge(options, this.__dynamicHelpers);260 }261262 // Merge view helpersAdded union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758
line 263why, one step back263 union(options, helpers);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 264–266why, one step back264265 // Always expose partial() as a local266 options.partial = function(path, opts){Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 267why, one step back267 return renderPartial(self, path, opts, options, view);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 268–300why, one step back268 };269270 function error(err) {271 if (fn) {272 fn(err);273 } else {274 self.req.next(err);275 }276 }277278 // Attempt render279 try {280 var engine = view.templateEngine281 , template = cacheTemplates282 ? cache[view.path] || (cache[view.path] = engine.compile(view.contents, options))283 : engine.compile(view.contents, options)284 , str = template.call(options.scope, options);285 } catch (err) {286 return error(err);287 }288289 // Layout support290 if (layout) {291 options.isLayout = true;292 options.layout = false;293 options.body = str;294 options.relative = false;295 self.render(layout, options, fn, view);296 } else if (partial) {297 return str;298 } else if (fn) {299 fn(null, str);300 } else {Removed partial() / render() status and header optionsTj Holowaychukbefore adoptionaaa8415
line 301why, one step back301 this.send(str);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 302–303why, one step back302 }303};