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
Provenance
- Commit
20b8fac- Tree
96aa687- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
909914f
1 file changed+17 added−5 removed
modified:docs/guide.md+17 added-5 removed
521 unmodified lines522 523 ### View Partials524 525 The Express view system has built-in support for partials and collections, which are "mini" views representing a document fragment. For example rather than iterating526-in a view to display comments, we would use a partial with collection support:526+in a view to display comments, we could use partial collection:527 528 partial('comment', { collection: comments });529 530-If no other options are desired, we can omit the object and simply pass our array, which is equivalent to above:530+If no other options or local variables are desired, we can omit the object and simply pass our array, which is equivalent to above:531 532 partial('comment', comments);533 534-When using the partial collection support a few "magic" variables are provided534+When using the partial collection support a few "magic" locals are provided535 for free:536 537 * _firstInCollection_ true if this is the first object538 * _indexInCollection_ index of the object in the collection398 unmodified lines937 938 partial('movie', { collection: movies, as: this });939 // In view: this.director940 941-Another alternative is to "explode" the properties of the collection item into941+Another alternative is to "expand" the properties of the collection item into942 pseudo globals (local variables) by using _as: global_, which again is syntactic sugar:943 944 partial('movie', { collection: movies, as: global });945 // In view: director13 unmodified lines959 // In view: movie.director960 961 When 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:962 963+ var movie = new Movie('Nightmare Before Christmas', 'Tim Burton')963- partial('movie', movie);964+ partial('movie', movie)965 // => In view: movie.director966 967+The 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:968+969+ var movie = { title: 'Nightmare Before Christmas', director: 'Tim Burton' }; 970+ partial('movie', movie)971+972+For 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:973+974+ partial('movie', { locals: { movie: movie }})975+ partial('movie', { movie: movie })976+ partial('movie', { object: movie })977+978 This 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:979 980 app.get('/users', function(req, res){981 if (req.xhr) {239 unmodified lines