Skip to content
Myr

Added partial() support for array-like collections. Closes #434

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 · parent 27fa3ba · Browse files · 2 files+42 added−5 removed
This commit

State

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

Diff stats

2 files+42 added−5 removed

  • tests:+34 added−0 removed
  • other:+8 added−5 removed

2 files+42 added−5 removed

modified:lib/express/view.js+8 added-5 removed
115 unmodified lines
116 view += ext;117 }118119 // Allow collection to be passed as second param120 if (Array.isArray(options)) {120 if (options && options.hasOwnProperty('length')) {121 options = { collection: options };122 }123124 // Defaults
5 unmodified lines
130 // Collection support131 var collection = options.collection;132 if (collection) {133 var name = options.as || view.split('.')[0],134 len = collection.length;134 len = collection.length,135 buf = '';136 delete options.collection;137 options.locals.collectionLength = len;137 return collection.map(function(val, i){138 for (var i = 0, len = collection.length; 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;143 options.object = val;142 return this.partial(view, options);143 }, this).join('');144 buf += this.partial(view, options);145 }146 return buf;147 } else {148 if (options.object) {149 var name = options.as || view.split('.')[0];150 if (typeof name === 'string') {
161 unmodified lines
modified:test/view.test.js+34 added
434 unmodified lines
435 { url: '/nothing' },436 { body: 'Hello' });437 },438 439 'test #partial() with array-like collection': function(assert){440 var app = create();441442 var movies = {443 0: { title: 'Nightmare Before Christmas', director: 'Tim Burton' },444 1: { title: 'Avatar', director: 'James Cameron' },445 length: 2446 };447 app.get('/movies', function(req, res){448 res.render('movies.jade', { locals: { movies: movies }});449 });450 451 var html = [452 '<html>',453 '<body>',454 '<ul>',455 '<li>',456 '<div class="title">Nightmare Before Christmas</div>',457 '<div class="director">Tim Burton</div>',458 '</li>',459 '<li>',460 '<div class="title">Avatar</div>',461 '<div class="director">James Cameron</div>',462 '</li>',463 '</ul>',464 '</body>',465 '</html>'466 ].join('');467468 assert.response(app,469 { url: '/movies' },470 { body: html });471 },472 473 'test "partials" setting': function(assert){474 var app = create();475 app.set('partials', __dirname + '/fixtures/sub-templates');476
64 unmodified lines