view.js
This file
Path
lib/view.js
323 lines · 7.9 KiB · JavaScript
Last changed by
Added charset option for render()Tj Holowaychukbefore adoptionf327455
History of this fileTake it
moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 1–17why, 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')Removed Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb
line 18why, one step back18 , 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–44why, one step back2324/**25 * Memory cache.26 *27 * @type Object28 */2930var cache = {};3132/**33 * Expose constructors.34 */3536exports = module.exports = View;3738/**39 * Export template engine registrar.40 */4142exports.register = View.register;4344/**Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 45why, one step back45 * Partial render helper.moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
line 46why, one step back46 *Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 47why, one step back47 * @api privatemoved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 48–49why, one step back48 */49Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 50why, one step back50function renderPartial(res, view, options, locals, parent){moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 51–67why, one step back51 // Inherit parent view extension when not present52 if (parent && !~view.indexOf('.')) {53 view += parent.extension;54 }5556 // Allow collection to be passed as second param57 if (options) {58 if ('length' in options) {59 options = { collection: options };60 } else if (!options.collection && !options.locals && !options.object) {61 options = { object: options };62 }63 } else {64 options = {};65 }6667 // Inherit locals from parentFixed partial() issue where the parent locals clobber those given directlyTj Holowaychukbefore adoption1fc520b
line 68why, one step back68 union(options, locals);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 69–79why, one step back69 70 // Merge locals71 if (options.locals) {72 merge(options, options.locals);73 }7475 // Partials dont need layouts76 options.renderPartial = true;77 options.layout = false;7879 // Deduce name from view pathRemoved Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb
line 80why, one step back80 var name = options.as || partial.resolveObjectName(view);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 81–92why, one step back8182 // Render partial83 function render(){84 if (options.object) {85 if ('string' == typeof name) {86 options[name] = options.object;87 } else if (name === global) {88 merge(options, options.object);89 } else {90 options.scope = options.object;91 }92 }Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 93why, one step back93 return res.render(view, options, null, parent);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 94–117why, one step back94 }9596 // Collection support97 var collection = options.collection;98 if (collection) {99 var len = collection.length100 , buf = '';101 delete options.collection;102 options.collectionLength = len;103 for (var i = 0; i < len; ++i) {104 var val = collection[i];105 options.firstInCollection = i === 0;106 options.indexInCollection = i;107 options.lastInCollection = i === len - 1;108 options.object = val;109 buf += render();110 }111 return buf;112 } else {113 return render();114 }115};116117/**Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 118–151why, one step back118 * Render `view` partial with the given `options`.119 *120 * Options:121 *122 * - `object` Single object with name derived from the view (unless `as` is present) 123 *124 * - `as` Variable name for each `collection` value, defaults to the view name.125 * * as: 'something' will add the `something` local variable126 * * as: this will use the collection value as the template context127 * * as: global will merge the collection value's properties with `locals`128 *129 * - `collection` Array of objects, the name is derived from the view name itself. 130 * For example _video.html_ will have a object _video_ available to it.131 *132 * @param {String} view133 * @param {Object|Array} options, collection, or object134 * @return {String}135 * @api public136 */137138res.partial = function(view, options){139 var app = this.app140 , options = options || {}141 , parent = {};142143 // root "views" option144 parent.dirname = app.set('views') || process.cwd() + '/views';145146 // utilize "view engine" option147 if (app.set('view engine')) {148 parent.extension = '.' + app.set('view engine');149 }150151 var str = renderPartial(this, view, options, null, parent);Removed partial() / render() status and header optionsTj Holowaychukbefore adoptionaaa8415
line 152why, one step back152 this.send(str);Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 153–155why, one step back153};154155/**moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 156–163why, one step back156 * Render `view` with the given `options` and optional callback `fn`.157 * When a callback function is given a response will _not_ be made158 * automatically, however otherwise a response of _200_ and _text/html_ is given.159 *160 * Options:161 * 162 * - `scope` Template evaluation context (the value of `this`)163 * - `debug` Output debugging informationdocsTj Holowaychukbefore adoption2007407
line 164why, one step back164 * - `status` Response status codemoved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 165–171why, one step back165 *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 back194Added res.render() .locals support back to aid in migration processTj Holowaychukbefore adoption07d06ad
lines 195–197why, one step back195 // merge render() .locals196 if (opts && opts.locals) merge(options, opts.locals);197Added res.render() status support backTj Holowaychukbefore adoptiond152e7e
lines 198–200why, one step back198 // status support199 if (options.status) this.statusCode = options.status;200moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 201–202why, one step back201 // Defaults202 var self = thisutilizing app.set()Tj Holowaychukbefore adoption334604d
line 203why, one step back203 , root = app.set('views') || process.cwd() + '/views'moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 204–211why, one step back204 , partial = options.renderPartial205 , layout = options.layout;206207 // Layout support208 if (true === layout || undefined === layout) {209 layout = 'layout';210 }211Changed default view scope to a plain objectTj Holowaychukbefore adoptiona49e1f4
lines 212–213why, one step back212 // Default execution scope to a plain object213 options.scope = options.scope || {};moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 214–221why, one step back214215 // Populate view216 options.parentView = parent;217218 // "views" setting219 options.root = root;220221 // "view engine" settingutilizing app.set()Tj Holowaychukbefore adoption334604d
line 222why, one step back222 options.defaultEngine = app.set('view engine');moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
line 223why, one step back223Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 224–226why, one step back224 // charset option225 if (options.charset) this.charset = options.charset;226moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
line 227why, one step back227 // Populate viewRemoved Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb
line 228why, one step back228 var orig = view = new View(view, options);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
line 229why, one step back229Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
lines 230–231why, one step back230 // Try _ prefix ex: ./views/_user.jade231 if (!view.exists) view = new View(orig.prefixPath, options);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
line 232why, one step back232 Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
line 233why, one step back233 // Try index ex: ./views/user/index.jademisc refactorTj Holowaychukbefore adoption110b0fe
line 234why, one step back234 if (!view.exists) view = new View(orig.indexPath, options);Added View#upIndexPathTj Holowaychukbefore adoption32be4a7
line 235why, one step back235Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
line 236why, one step back236 // Try ../name ../user from within ./usermisc refactorTj Holowaychukbefore adoption110b0fe
line 237why, one step back237 if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);fixing testsTj Holowaychukbefore adoption2b907e9
line 238why, one step back238Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6
lines 239–240why, one step back239 // Try layout relative to the "views" dir 240 if (!view.exists && options.isLayout) view = new View(orig.rootPath, options);Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
lines 241–243why, one step back241242 // Does not exist243 if (!view.exists) {hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c
lines 244–245why, one step back244 if (app.enabled('hints')) hintAtViewPaths(orig, options);245 throw new Error('failed to locate view "' + orig.view + '"');Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
line 246why, one step back246 }Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6
line 247why, one step back247moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 248–265why, one step back248 // Dynamic helper support249 if (false !== options.dynamicHelpers) {250 // cache251 if (!this.__dynamicHelpers) {252 this.__dynamicHelpers = {};253 for (var key in dynamicHelpers) {254 this.__dynamicHelpers[key] = dynamicHelpers[key].call(255 this.app256 , this.req257 , this);258 }259 }260261 // apply262 merge(options, this.__dynamicHelpers);263 }264265 // Merge view helpersAdded union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758
line 266why, one step back266 union(options, helpers);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 267–269why, one step back267268 // Always expose partial() as a local269 options.partial = function(path, opts){Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 270why, one step back270 return renderPartial(self, path, opts, options, view);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 271–302why, one step back271 };272273 function error(err) {274 if (fn) {275 fn(err);276 } else {277 self.req.next(err);278 }279 }280281 // Attempt render282 try {283 var engine = view.templateEngine284 , template = cacheTemplates285 ? cache[view.path] || (cache[view.path] = engine.compile(view.contents, options))286 : engine.compile(view.contents, options)287 , str = template.call(options.scope, options);288 } catch (err) {289 return error(err);290 }291292 // Layout support293 if (layout) {294 options.isLayout = true;295 options.layout = false;296 options.body = str;297 self.render(layout, options, fn, view);298 } else if (partial) {299 return str;300 } else if (fn) {301 fn(null, str);302 } else {Removed partial() / render() status and header optionsTj Holowaychukbefore adoptionaaa8415
line 303why, one step back303 this.send(str);moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 304–305why, one step back304 }305};hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c
lines 306–323why, one step back306307/**308 * Hint at view path resolution, outputting the309 * paths that Express has tried.310 *311 * @api private312 */313314function hintAtViewPaths(view, options) {315 console.error();316 console.error('failed to locate view "' + view.view + '", tried:');317 console.error(' - ' + new View(view.path, options).path);318 console.error(' - ' + new View(view.prefixPath, options).path);319 console.error(' - ' + new View(view.indexPath, options).path);320 if (!options.isLayout) console.error(' - ' + new View(view.upIndexPath, options).path);321 if (options.isLayout) console.error(' - ' + new View(view.rootPath, options).path);322 console.error();323}