Merge branch 'partials-inherit-locals'
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 · parents 3914895, 2fc9a81 · Browse files · 5 files·+56 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
5 files+56 added−10 removed
- tests:+39 added−0 removed
- other:+17 added−10 removed
Provenance
- Commit
e39fd1d- Tree
7021aa6- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
3914895- Parent 2
2fc9a81
5 files changed+56 added−10 removed
modified:lib/express/view.js+17 added-10 removed
8 unmodified lines9 * Module dependencies.10 */11 12 var extname = require('path').extname13- , utils = require('connect').utils14- , http = require('http')15- , fs = require('fs')16- , mime = utils.mime;13+ , utils = require('connect').utils14+ , http = require('http')15+ , fs = require('fs')16+ , mime = utils.mime;17 18 /**19 * Cache supported template engine exports to20 * increase performance by lowering the number88 unmodified lines109 * @return {String}110 * @api public111 */112 113-http.ServerResponse.prototype.partial = function(view, options, ext){113+http.ServerResponse.prototype.partial = function(view, options, ext, locals){114 // Inherit parent view extension when not present115 if (ext && view.indexOf('.') < 0) {116 view += ext;117 }4 unmodified lines122 }123 124 // Defaults125 options = options || {};126+127+ // Inherit locals from parent128 options.locals = options.locals || {};129+ utils.merge(options.locals, locals);130+131+ // Partials dont need layouts132 options.partial = true;133 options.layout = false;134+135+ // Deduce name from view path136 var name = options.as || view.split('/').slice(-1)[0].split('.')[0];137 138 // Collection support139 var collection = options.collection;101 unmodified lines241 ? view242 : root + '/' + view;243 244 // Pass filename to the engine and view238- options.locals = options.locals || {};245+ var locals = options.locals = options.locals || {};246 options.locals.__filename = options.filename = path;247 248 // Dynamic helper support249 if (options.dynamicHelpers !== false) {16 unmodified lines266 // apply267 utils.merge(helpers, this.__dynamicHelpers);268 }269 270+ // Merge view helpers271+ options.locals.__proto__ = helpers;272+273 // Always expose partial() as a local274 options.locals.partial = function(view, options){265- return self.partial.call(self, view, options, ext);275+ return self.partial.call(self, view, options, ext, locals);276 };267- 268- // Merge view helpers269- options.locals.__proto__ = helpers;277 278 function error(err) {279 if (fn) {280 fn(err);36 unmodified linesadded:test/fixtures/partials/pet-count.jade+1 added
1+| We have #{pets.length} cool petsadded:test/fixtures/partials/pet.jade+4 added
1+- if (firstInCollection)2+ li #{pet} is the coolest of #{site}3+- else4+ li= petadded:test/fixtures/pets.jade+3 added
1+h1= site2+p!= partial('pet-count')3+ul!= partial('pet', pets)modified:test/view.test.js+31 added
464 unmodified lines465 { url: '/role' },466 { body: '<li>Role: admin</li><li>Role: member</li>' });467 },468 469+ 'test #partial() inheriting initial locals': function(assert){470+ var app = create();471+472+ app.get('/pets', function(req, res, next){473+ res.render('pets.jade', {474+ locals: {475+ site: 'My Cool Pets',476+ pets: ['Tobi', 'Jane', 'Bandit']477+ }478+ });479+ });480+481+ var html = [482+ '<html>',483+ '<body>',484+ '<h1>My Cool Pets</h1>',485+ '<p>We have 3 cool pets\n</p>',486+ '<ul>',487+ '<li>Tobi is the coolest of My Cool Pets</li>',488+ '<li>Jane</li>',489+ '<li>Bandit</li>',490+ '</ul>',491+ '</body>',492+ '</html>'493+ ].join('');494+495+ assert.response(app,496+ { url: '/pets' },497+ { body: html });498+ },499+ 500 'test #partial() with array-like collection': function(assert){501 var app = create();502 503 var movies = {98 unmodified lines