Skip to content
Myr

Deduce partial object names from the last segment

Before adoption: this commit came in with the history the project adopted, so no landing records it.

user/role.ejs will use "role" as the object name by default user.ejs will use "user" forum/thread/post.jade will use "post" etc

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 1f78c52 · Browse files · 3 files+25 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+25 added−7 removed

  • tests:+21 added−2 removed
  • other:+4 added−5 removed

3 files+25 added−7 removed

modified:lib/express/view.js+4 added-5 removed
124 unmodified lines
125 options = options || {};126 options.locals = options.locals || {};127 options.partial = true;128 options.layout = false;129 var name = options.as || view.split('/').slice(-1)[0].split('.')[0];130131 // Collection support132 var collection = options.collection;133 if (collection) {133 var name = options.as || view.split('.')[0]134 , len = collection.length134 var len = collection.length135 , buf = '';136 delete options.collection;137 options.locals.collectionLength = len;138 for (var i = 0, len = collection.length; i < len; ++i) {138 for (var i = 0; i < len; ++i) {139 var val = collection[i];140 options.locals.firstInCollection = i === 0;141 options.locals.indexInCollection = i;142 options.locals.lastInCollection = i === len - 1;
2 unmodified lines
145 }146 return buf;147 } else {148 if (options.object) {149 var name = options.as || view.split('.')[0];150 if (typeof name === 'string') {149 if ('string' == typeof name) {150 options.locals[name] = options.object;151 } else if (name === global) {152 utils.merge(options.locals, options.object);153 } else {
156 unmodified lines
added:test/fixtures/partials/user/role.ejs+1 added
1<li>Role: <%= role %></li>
modified:test/view.test.js+20 added-2 removed
360 unmodified lines
361 }));362 });363 364 assert.response(app,365 { url: '/user' },366 { body: '<p>tj</p>' });365 { url: '/user' },366 { body: '<p>tj</p>' });367 368 // as: this collection option369 app.get('/person', function(req, res){370 res.send(res.partial('person.jade', {
74 unmodified lines
445 446 assert.response(app,447 { url: '/nothing' },448 { body: 'Hello' });449450 // Path segments + "as"451 app.get('/role/as', function(req, res){452 res.send(res.partial('user/role.ejs', { as: 'role', collection: ['admin', 'member'] }));453 });454455 assert.response(app,456 { url: '/role/as' },457 { body: '<li>Role: admin</li><li>Role: member</li>' });458459 // Deduce name from last segment460 app.get('/role', function(req, res){461 res.send(res.partial('user/role.ejs', ['admin', 'member']));462 });463464 assert.response(app,465 { url: '/role' },466 { body: '<li>Role: admin</li><li>Role: member</li>' });467 },468 469 'test #partial() with array-like collection': function(assert){470 var app = create();
100 unmodified lines