Skip to content
Myr

Added support for "as" for non-collection partials. Closes #341

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 afbfd4d · Browse files · 2 files+41 added−4 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+41 added−4 removed

  • tests:+28 added−4 removed
  • other:+13 added−0 removed

2 files+41 added−4 removed

modified:lib/express/view.js+13 added
130 unmodified lines
131/**132 * Render `view` partial with the given `options`.133 *134 * Options:135 * - `object` Single object with name derived from the view (unless `as` is present) 136 *137 * - `as` Variable name for each `collection` value, defaults to the view name.138 * * as: 'something' will add the `something` local variable139 * * as: this will use the collection value as the template context140 * * as: global will merge the collection value's properties with `locals`
36 unmodified lines
177 locals.lastInCollection = i === len - 1;178 return this.render(view, options);179 }, this).join('');180 } else {181 options.locals = options.locals || {};182 if (options.object) {183 var name = options.as || view.split('.')[0];184 if (typeof name === 'string') {185 options.locals[name] = options.object;186 } else if (name === global) {187 utils.merge(options.locals, options.object);188 } else {189 options.context = options.object;190 }191 }192 return this.render(view, options);193 }194};195
116 unmodified lines
modified:test/view.test.js+28 added-4 removed
190 unmodified lines
191 { url: '/magic' },192 { body: '<li class="first">one</li><li class="word-1">two</li><li class="last">three</li>' });193 194 // Non-collection support195 app.get('/video', function(req, res){196 res.send(res.partial('video.jade', {197 locals: { director: movies[0].director }195 app.get('/movie', function(req, res){196 res.send(res.partial('movie.jade', {197 object: movies[0]198 }));199 });200 201 assert.response(app,202 { url: '/video' },202 { url: '/movie' },203 { body: '<li><div class="title">Nightmare Before Christmas</div><div class="director">Tim Burton</div></li>' });204 205 app.get('/video-global', function(req, res){206 res.send(res.partial('video.jade', {207 object: movies[0],208 as: global209 })); 210 });211 212 // Non-collection as: global213 assert.response(app,214 { url: '/video-global' },215 { body: '<p>Tim Burton</p>' });216217 app.get('/person-this', function(req, res){218 res.send(res.partial('person.jade', {219 object: { name: 'tj' },220 as: this221 })); 222 });223 224 // Non-collection as: this225 assert.response(app,226 { url: '/person-this' },227 { body: '<p>tj</p>' });228 }229};