Skip to content
Myr

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

6 files+174 added−3 removed

modified:docs/guide.1+73 added
342 unmodified lines
343.fi344.345.IP "" 0346.347.SS "View Rendering"348View 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.P351Below 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.nf356357app\.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"368The 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.nf373374partial(\'comment\.haml\', { collection: comments });375.376.fi377.378.IP "" 0379.380.P381To 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.nf386387partial(\'comment\', { collection: comments });388.389.fi390.391.IP "" 0392.393.P394And 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.nf399400partial(\'comments\', comments);401.402.fi403.404.IP "" 0405.406.SS "Template Engines"407Below are a few template engines commonly used with Express:408.409.IP "\(bu" 4410Jade \fIhttp://github\.com/visionmedia/jade\fR haml\.js successor411.412.IP "\(bu" 4413Haml \fIhttp://github\.com/visionmedia/haml\.js\fR indented templates414.415.IP "\(bu" 4416EJS \fIhttp://github\.com/visionmedia/ejs\fR Embedded JavaScript417.418.IP "" 0419.420.SS "req\.header(key[, defaultValue])"421Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:422.423.IP "" 4
405 unmodified lines
modified:docs/guide.html+54 added
172 unmodified lines
173<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 lines
439 });440});441</code></pre>442443<h3 id="View-Rendering">View Rendering</h3>444445<p>View filenames take the form <em>Express</em>.<em>ENGINE</em>, where <em>ENGINE</em> is the name446of the module that will be required. For example the view <em>layout.ejs</em> will447tell the view system to <em>require('ejs')</em>, the module being loaded must (currently)448export the method <em>exports.render(str, options)</em> to comply with Express, however449with will likely be extensible in the future.</p>450451<p>Below is an example using <a href="http://github.com/visionmedia/haml.js">Haml.js</a> to render <em>index.html</em>,452and since we do not use <em>layout: false</em> the rendered contents of <em>index.html</em> will be passed as453the <em>body</em> local variable in <em>layout.haml</em>.</p>454455<pre><code>app.get('/', function(req, res){456 res.render('index.haml', {457 locals: { title: 'My Site' }458 });459});460</code></pre>461462<h3 id="View-Partials">View Partials</h3>463464<p>The Express view system has built-in support for partials and collections, which are465sort of "mini" views representing a document fragment. For example rather than iterating466in a view to display comments, we would use a partial with collection support:</p>467468<pre><code>partial('comment.haml', { collection: comments });469</code></pre>470471<p>To make things even less verbose we can assume the extension as <em>.haml</em> when omitted,472however if we wished we could use an ejs partial, within a haml view for example.</p>473474<pre><code>partial('comment', { collection: comments });475</code></pre>476477<p>And once again even further, when rendering a collection we can simply pass478an array, if no other options are desired:</p>479480<pre><code>partial('comments', comments);481</code></pre>482483<h3 id="Template-Engines">Template Engines</h3>484485<p>Below are a few template engines commonly used with Express:</p>486487<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>492493494<h3 id="req-header-key-defaultValue-">req.header(key[, defaultValue])</h3>495496<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>497
271 unmodified lines
modified:docs/guide.md+44 added
208 unmodified lines
209 } 210 });211 });212213### View Rendering214215View filenames take the form _NAME_._ENGINE_, where _ENGINE_ is the name216of the module that will be required. For example the view _layout.ejs_ will217tell the view system to _require('ejs')_, the module being loaded must (currently)218export the method _exports.render(str, options)_ to comply with Express, however 219with will likely be extensible in the future.220221Below is an example using [Haml.js](http://github.com/visionmedia/haml.js) to render _index.html_,222and since we do not use _layout: false_ the rendered contents of _index.html_ will be passed as 223the _body_ local variable in _layout.haml_.224225 app.get('/', function(req, res){226 res.render('index.haml', {227 locals: { title: 'My Site' }228 });229 });230231### View Partials232233The Express view system has built-in support for partials and collections, which are234sort of "mini" views representing a document fragment. For example rather than iterating235in a view to display comments, we would use a partial with collection support:236237 partial('comment.haml', { collection: comments });238239To make things even less verbose we can assume the extension as _.haml_ when omitted,240however if we wished we could use an ejs partial, within a haml view for example.241242 partial('comment', { collection: comments });243244And once again even further, when rendering a collection we can simply pass245an array, if no other options are desired:246247 partial('comments', comments);248249### Template Engines250251Below are a few template engines commonly used with Express:252253 * [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 JavaScript256257### req.header(key[, defaultValue])258259Get the case-insensitive request header _key_, with optional _defaultValue_:260
239 unmodified lines
modified:docs/migrate.1+1 added-1 removed
275 unmodified lines
276.277.IP "" 0278.279.P280To make things even less verbose we can assume the extension when ommitted:280To make things even less verbose we can assume the extension when omitted:281.282.IP "" 4283.284.nf
63 unmodified lines
modified:docs/migrate.html+1 added-1 removed
352 unmodified lines
353354<pre><code>partial('comment.haml', { collection: comments });355</code></pre>356357<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>358359<pre><code>partial('comment', { collection: comments });360</code></pre>361
37 unmodified lines
modified:docs/migrate.md+1 added-1 removed