Added as: this support

This commit is contained in:
Tj Holowaychuk 2010-07-08 10:17:11 -07:00
parent 5544c5d5d4
commit edfb18b75a
3 changed files with 22 additions and 2 deletions

View File

@ -46,7 +46,10 @@ exports.clearCache = function(){
* Render `view` partial with the given `options`.
*
* Options:
* - `as` Variable name for each `collection` value, defaults to the view name.
* - `as` Variable name for each `collection` value, defaults to the view name.
* When assigning `as: this`, each value in the collection will become the
* evaluation context of the template.
*
* - `collection` Array of objects, the name is derived from the view name itself.
* For example _video.html_ will have a object _video_ available to it.
*
@ -70,7 +73,11 @@ http.ServerResponse.prototype.partial = function(view, options){
var locals = options.locals = options.locals || {};
locals.collectionLength = len;
return collection.map(function(val, i){
locals[name] = val;
if (typeof name === 'string') {
locals[name] = val;
} else {
options.context = val;
}
locals.firstInCollection = i === 0;
locals.indexInCollection = i;
locals.lastInCollection = i === len - 1;

1
test/fixtures/partials/person.jade vendored Normal file
View File

@ -0,0 +1 @@
p= this.name

View File

@ -106,5 +106,17 @@ module.exports = {
assert.response(app,
{ url: '/user' },
{ body: '<p>tj</p>' });
// "as" this collection option
app.get('/person', function(req, res){
res.send(res.partial('person.jade', {
as: this,
collection: [{ name: 'tj' }]
}));
});
assert.response(app,
{ url: '/person' },
{ body: '<p>tj</p>' });
}
};