mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 12:19:51 +01:00
Added as: this support
This commit is contained in:
parent
5544c5d5d4
commit
edfb18b75a
|
|
@ -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
1
test/fixtures/partials/person.jade
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
p= this.name
|
||||
|
|
@ -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>' });
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user