Added more view docs
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 f904606 · Browse files · 6 files·+174 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+174 added−3 removed
- other:+174 added−3 removed
Provenance
- Commit
2f96506- Tree
6fccd36- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
f904606
6 files changed+174 added−3 removed
modified:docs/guide.1+73 added
342 unmodified lines343 .fi344 .345 .IP "" 0346 .347+.SS "View Rendering"348+View filenames take the form \fINAME\fR\.\fIENGINE\fR, where \fIENGINE\fR is the name of the module that will be required\. For example the view \fIlayout\.ejs\fR will tell the view system to \fIrequire(\'ejs\')\fR, the module being loaded must (currently) export the method \fIexports\.render(str, options)\fR to comply with Express, however with will likely be extensible in the future\.349+.350+.P351+Below is an example using Haml\.js \fIhttp://github\.com/visionmedia/haml\.js\fR to render \fIindex\.html\fR, and since we do not use \fIlayout: false\fR the rendered contents of \fIindex\.html\fR will be passed as the \fIbody\fR local variable in \fIlayout\.haml\fR\.352+.353+.IP "" 4354+.355+.nf356+357+app\.get(\'/\', function(req, res){358+ res\.render(\'index\.haml\', {359+ locals: { title: \'My Site\' }360+ });361+});362+.363+.fi364+.365+.IP "" 0366+.367+.SS "View Partials"368+The Express view system has built\-in support for partials and collections, which are sort of \"mini\" views representing a document fragment\. For example rather than iterating in a view to display comments, we would use a partial with collection support:369+.370+.IP "" 4371+.372+.nf373+374+partial(\'comment\.haml\', { collection: comments });375+.376+.fi377+.378+.IP "" 0379+.380+.P381+To make things even less verbose we can assume the extension as \fI\.haml\fR when omitted, however if we wished we could use an ejs partial, within a haml view for example\.382+.383+.IP "" 4384+.385+.nf386+387+partial(\'comment\', { collection: comments });388+.389+.fi390+.391+.IP "" 0392+.393+.P394+And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:395+.396+.IP "" 4397+.398+.nf399+400+partial(\'comments\', comments);401+.402+.fi403+.404+.IP "" 0405+.406+.SS "Template Engines"407+Below are a few template engines commonly used with Express:408+.409+.IP "\(bu" 4410+Jade \fIhttp://github\.com/visionmedia/jade\fR haml\.js successor411+.412+.IP "\(bu" 4413+Haml \fIhttp://github\.com/visionmedia/haml\.js\fR indented templates414+.415+.IP "\(bu" 4416+EJS \fIhttp://github\.com/visionmedia/ejs\fR Embedded JavaScript417+.418+.IP "" 0419+.420 .SS "req\.header(key[, defaultValue])"421 Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:422 .423 .IP "" 4405 unmodified linesmodified:docs/guide.html+54 added
172 unmodified lines173 <li><a href="#Routing">Routing</a></li>174 <li><a href="#Passing-Route-Control">Passing Route Control</a></li>175 <li><a href="#Middleware">Middleware</a></li>176 <li><a href="#Error-Handling">Error Handling</a></li>177+<li><a href="#View-Rendering">View Rendering</a></li>178+<li><a href="#View-Partials">View Partials</a></li>179+<li><a href="#Template-Engines">Template Engines</a></li>180 <li><a href="#req-header-key-defaultValue-">req.header()</a></li>181 <li><a href="#req-accepts-type-">req.accepts()</a></li>182 <li><a href="#req-param-name-">req.param()</a></li>183 <li><a href="#req-flash-type-msg-">req.flash()</a></li>255 unmodified lines439 });440 });441 </code></pre>442 443+<h3 id="View-Rendering">View Rendering</h3>444+445+<p>View filenames take the form <em>Express</em>.<em>ENGINE</em>, where <em>ENGINE</em> is the name446+of the module that will be required. For example the view <em>layout.ejs</em> will447+tell the view system to <em>require('ejs')</em>, the module being loaded must (currently)448+export the method <em>exports.render(str, options)</em> to comply with Express, however449+with will likely be extensible in the future.</p>450+451+<p>Below is an example using <a href="http://github.com/visionmedia/haml.js">Haml.js</a> to render <em>index.html</em>,452+and since we do not use <em>layout: false</em> the rendered contents of <em>index.html</em> will be passed as453+the <em>body</em> local variable in <em>layout.haml</em>.</p>454+455+<pre><code>app.get('/', function(req, res){456+ res.render('index.haml', {457+ locals: { title: 'My Site' }458+ });459+});460+</code></pre>461+462+<h3 id="View-Partials">View Partials</h3>463+464+<p>The Express view system has built-in support for partials and collections, which are465+sort of "mini" views representing a document fragment. For example rather than iterating466+in a view to display comments, we would use a partial with collection support:</p>467+468+<pre><code>partial('comment.haml', { collection: comments });469+</code></pre>470+471+<p>To make things even less verbose we can assume the extension as <em>.haml</em> when omitted,472+however if we wished we could use an ejs partial, within a haml view for example.</p>473+474+<pre><code>partial('comment', { collection: comments });475+</code></pre>476+477+<p>And once again even further, when rendering a collection we can simply pass478+an array, if no other options are desired:</p>479+480+<pre><code>partial('comments', comments);481+</code></pre>482+483+<h3 id="Template-Engines">Template Engines</h3>484+485+<p>Below are a few template engines commonly used with Express:</p>486+487+<ul>488+<li><a href="http://github.com/visionmedia/jade">Jade</a> haml.js successor</li>489+<li><a href="http://github.com/visionmedia/haml.js">Haml</a> indented templates</li>490+<li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>491+</ul>492+493+494 <h3 id="req-header-key-defaultValue-">req.header(key[, defaultValue])</h3>495 496 <p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>497 271 unmodified linesmodified:docs/guide.md+44 added
208 unmodified lines209 } 210 });211 });212 213+### View Rendering214+215+View filenames take the form _NAME_._ENGINE_, where _ENGINE_ is the name216+of the module that will be required. For example the view _layout.ejs_ will217+tell the view system to _require('ejs')_, the module being loaded must (currently)218+export the method _exports.render(str, options)_ to comply with Express, however 219+with will likely be extensible in the future.220+221+Below is an example using [Haml.js](http://github.com/visionmedia/haml.js) to render _index.html_,222+and since we do not use _layout: false_ the rendered contents of _index.html_ will be passed as 223+the _body_ local variable in _layout.haml_.224+225+ app.get('/', function(req, res){226+ res.render('index.haml', {227+ locals: { title: 'My Site' }228+ });229+ });230+231+### View Partials232+233+The Express view system has built-in support for partials and collections, which are234+sort of "mini" views representing a document fragment. For example rather than iterating235+in a view to display comments, we would use a partial with collection support:236+237+ partial('comment.haml', { collection: comments });238+239+To make things even less verbose we can assume the extension as _.haml_ when omitted,240+however if we wished we could use an ejs partial, within a haml view for example.241+242+ partial('comment', { collection: comments });243+244+And once again even further, when rendering a collection we can simply pass245+an array, if no other options are desired:246+247+ partial('comments', comments);248+249+### Template Engines250+251+Below are a few template engines commonly used with Express:252+253+ * [Jade](http://github.com/visionmedia/jade) haml.js successor254+ * [Haml](http://github.com/visionmedia/haml.js) indented templates255+ * [EJS](http://github.com/visionmedia/ejs) Embedded JavaScript256+257 ### req.header(key[, defaultValue])258 259 Get the case-insensitive request header _key_, with optional _defaultValue_:260 239 unmodified linesmodified:docs/migrate.1+1 added-1 removed
275 unmodified lines276 .277 .IP "" 0278 .279 .P280-To make things even less verbose we can assume the extension when ommitted:280+To make things even less verbose we can assume the extension when omitted:281 .282 .IP "" 4283 .284 .nf63 unmodified linesmodified:docs/migrate.html+1 added-1 removed
352 unmodified lines353 354 <pre><code>partial('comment.haml', { collection: comments });355 </code></pre>356 357-<p>To make things even less verbose we can assume the extension when ommitted:</p>357+<p>To make things even less verbose we can assume the extension when omitted:</p>358 359 <pre><code>partial('comment', { collection: comments });360 </code></pre>361 37 unmodified linesmodified:docs/migrate.md+1 added-1 removed
157 unmodified linesExpand all15815916016116216216316416516626 unmodified lines157 unmodified linesExpand alllooks like this, as _partial()_ is _always_ passed as a local variable:
partial('comment.haml', { collection: comments });
To make things even less verbose we can assume the extension when ommitted:To make things even less verbose we can assume the extension when omitted:
partial('comment', { collection: comments });
And once again even further, when rendering a collection we can simply pass26 unmodified lines