Skip to content
Myr

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

5 files+56 added−10 removed

modified:lib/express/view.js+17 added-10 removed
8 unmodified lines
9 * Module dependencies.10 */1112var 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;1718/**19 * Cache supported template engine exports to20 * increase performance by lowering the number
88 unmodified lines
109 * @return {String}110 * @api public111 */112113http.ServerResponse.prototype.partial = function(view, options, ext){113http.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 lines
122 }123124 // Defaults125 options = options || {};126127 // Inherit locals from parent128 options.locals = options.locals || {};129 utils.merge(options.locals, locals);130131 // Partials dont need layouts132 options.partial = true;133 options.layout = false;134135 // Deduce name from view path136 var name = options.as || view.split('/').slice(-1)[0].split('.')[0];137138 // Collection support139 var collection = options.collection;
101 unmodified lines
241 ? view242 : root + '/' + view;243244 // 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;247248 // Dynamic helper support249 if (options.dynamicHelpers !== false) {
16 unmodified lines
266 // apply267 utils.merge(helpers, this.__dynamicHelpers);268 }269270 // Merge view helpers271 options.locals.__proto__ = helpers;272273 // 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;277278 function error(err) {279 if (fn) {280 fn(err);
36 unmodified lines
added:test/fixtures/partials/pet-count.jade+1 added
1| We have #{pets.length} cool pets
added:test/fixtures/partials/pet.jade+4 added
1- if (firstInCollection)2 li #{pet} is the coolest of #{site}3- else4 li= pet
added:test/fixtures/pets.jade+3 added
1h1= site2p!= partial('pet-count')3ul!= partial('pet', pets)
modified:test/view.test.js+31 added
464 unmodified lines
465 { 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();471472 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 });480481 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('');494495 assert.response(app,496 { url: '/pets' },497 { body: html });498 },499 500 'test #partial() with array-like collection': function(assert){501 var app = create();502503 var movies = {
98 unmodified lines