Skip to content
Myr

docs for partial changes

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 909914f · Browse files · 1 file+17 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

1 file+17 added−5 removed

  • other:+17 added−5 removed

1 file+17 added−5 removed

modified:docs/guide.md+17 added-5 removed
521 unmodified lines
522523### View Partials524525The Express view system has built-in support for partials and collections, which are "mini" views representing a document fragment. For example rather than iterating526in a view to display comments, we would use a partial with collection support:526in a view to display comments, we could use partial collection:527528 partial('comment', { collection: comments });529530If no other options are desired, we can omit the object and simply pass our array, which is equivalent to above:530If no other options or local variables are desired, we can omit the object and simply pass our array, which is equivalent to above:531532 partial('comment', comments);533534When using the partial collection support a few "magic" variables are provided534When using the partial collection support a few "magic" locals are provided535for free:536537 * _firstInCollection_ true if this is the first object538 * _indexInCollection_ index of the object in the collection
398 unmodified lines
937938 partial('movie', { collection: movies, as: this });939 // In view: this.director940941Another alternative is to "explode" the properties of the collection item into941Another alternative is to "expand" the properties of the collection item into942pseudo globals (local variables) by using _as: global_, which again is syntactic sugar:943944 partial('movie', { collection: movies, as: global });945 // In view: director
13 unmodified lines
959 // In view: movie.director960961When a non-collection (does _not_ have _.length_) is passed as the second argument, it is assumed to be the _object_, after which the object's local variable name is derived from the view name:962963 var movie = new Movie('Nightmare Before Christmas', 'Tim Burton')963 partial('movie', movie);964 partial('movie', movie)965 // => In view: movie.director966967The exception of this, is when a "plain" object, aka "{}" or "new Object" is passed, which is considered an object with local variable. For example some may expect a "movie" local with the following, however since it is a plain object "director" and "title" are simply locals:968969 var movie = { title: 'Nightmare Before Christmas', director: 'Tim Burton' }; 970 partial('movie', movie)971972For cases like this where passing a plain object is desired, simply assign it to a key, or use the `object` key which will use the filename-derived variable name. The examples below are equivalent:973974 partial('movie', { locals: { movie: movie }})975 partial('movie', { movie: movie })976 partial('movie', { object: movie })977978This exact API can be utilized from within a route, to respond with a fragment via Ajax or WebSockets, for example we can render a collection of users directly from a route:979980 app.get('/users', function(req, res){981 if (req.xhr) {
239 unmodified lines