Added app.helpers() 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 9ef8ea9 · Browse files · 8 files·+113 added−60 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+113 added−60 removed
- other:+113 added−60 removed
Provenance
- Commit
7eb088b- Tree
3ff859c- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
9ef8ea9
8 files changed+113 added−60 removed
modified:docs/api.html+50 added
663 unmodified lines664 </td>665 </tr>666 <tr class="code">667 <td class="docs">668+<p>Register the given view helpers <code>obj</code>. This method669+can be called several times to apply additional helpers.</p>670+671+<h2></h2>672+673+<ul><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>674+</td>675+<td class="code">676+<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">helpers</span> = <span class="keyword">function</span>(<span class="variable">obj</span>){677+ <span class="keyword">var</span> <span class="variable">keys</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">obj</span>);678+ <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">len</span> = <span class="variable">keys</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; ++<span class="variable">i</span>) {679+ <span class="keyword">var</span> <span class="variable">key</span> = <span class="variable">keys</span>[<span class="variable">i</span>],680+ <span class="variable">val</span> = <span class="variable">obj</span>[<span class="variable">key</span>];681+ <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">val</span> === <span class="string">'function'</span>) {682+ <span class="variable">view</span>.<span class="variable">dynamicHelpers</span>[<span class="variable">key</span>] = <span class="variable">val</span>;683+ } <span class="keyword">else</span> {684+ <span class="variable">view</span>.<span class="variable">helpers</span>[<span class="variable">key</span>] = <span class="variable">val</span>;685+ }686+ }687+ <span class="keyword">return</span> <span class="this">this</span>;688+};</code></pre>689+</td>690+</tr>691+<tr class="code">692+<td class="docs">693 <p>Assign a custom exception handler callback <code>fn</code>.</p>694 695 <h2></h2>696 199 unmodified lines896 </td>897 </tr>898 <tr class="code">899 <td class="docs">900+<p>Dynamic view helper functions.</p>901+902+<ul><li><p><strong>type</strong>: <em>Object</em></p></li></ul>903+</td>904+<td class="code">905+<pre><code><span class="keyword">var</span> <span class="variable">dynamicHelpers</span> = <span class="variable">exports</span>.<span class="variable">dynamicHelpers</span> = {};</code></pre>906+</td>907+</tr>908+<tr class="code">909+<td class="docs">910 <p>Render <code>view</code> partial with the given <code>options</code>.</p>911 912 <h2>Options</h2>913 88 unmodified lines1002 <span class="variable">layout</span> = <span class="variable">layout</span> === <span class="variable">true</span>1003 ? <span class="string">'layout'</span> + <span class="variable">ext</span>1004 : <span class="variable">layout</span>;1005 1006+ <span class="comment">// Allow layout name without extension</span>1007+ <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">layout</span> === <span class="string">'string'</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">layout</span>.<span class="variable">indexOf</span>(<span class="string">'.'</span>) &<span class="variable">lt</span>; <span class="number integer">0</span>) {1008+ <span class="variable">layout</span> += <span class="variable">ext</span>;1009+ }1010+1011 <span class="comment">// Normalize "context" to "scope"</span>1012 <span class="variable">options</span>.<span class="variable">scope</span> = <span class="variable">options</span>.<span class="variable">scope</span> || <span class="variable">options</span>.<span class="variable">context</span>;1013 1014 <span class="comment">// Auto-cache in production</span>21 unmodified lines1036 <span class="comment">// Pass filename to the engine and view</span>1037 <span class="variable">options</span>.<span class="variable">locals</span> = <span class="variable">options</span>.<span class="variable">locals</span> || {};1038 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">__filename</span> = <span class="variable">options</span>.<span class="variable">filename</span> = <span class="variable">path</span>;1039 1040+ <span class="comment">// Dynamic helper support</span>1041+ <span class="keyword">var</span> <span class="variable">keys</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">dynamicHelpers</span>);1042+ <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">len</span> = <span class="variable">keys</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; ++<span class="variable">i</span>) {1043+ <span class="keyword">var</span> <span class="variable">key</span> = <span class="variable">keys</span>[<span class="variable">i</span>],1044+ <span class="variable">val</span> = <span class="variable">dynamicHelpers</span>[<span class="variable">key</span>];1045+ <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">val</span> === <span class="string">'function'</span>) {1046+ <span class="variable">helpers</span>[<span class="variable">key</span>] = <span class="variable">val</span>.<span class="variable">call</span>(<span class="this">this</span>.<span class="variable">app</span>, <span class="this">this</span>.<span class="variable">req</span>, <span class="this">this</span>, <span class="this">this</span>.<span class="variable">req</span>.<span class="variable">params</span>.<span class="variable">path</span>);1047+ }1048+ }1049+1050 <span class="comment">// Always expose partial() as a local</span>1051 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">partial</span> = <span class="keyword">function</span>(<span class="variable">view</span>, <span class="variable">options</span>){1052 <span class="keyword">return</span> <span class="variable">self</span>.<span class="variable">partial</span>.<span class="variable">call</span>(<span class="variable">self</span>, <span class="variable">view</span>, <span class="variable">options</span>, <span class="variable">ext</span>);1053 };46 unmodified linesmodified:docs/guide.1+21 added-29 removed
499 unmodified lines500 _lastInCollection _ True if this is the last object501 .502 .IP "" 0503 .504-.SS "View Helpers"505-View helpers are merged with all template local variables, exposing them as pseudo globals\. Below is an example of exposing \fIfoo()\fR to all views:506-.507-.IP "" 4508-.509-.nf510-511-var view = require(\'express/view\');512-view\.helpers\.foo = function(){513- return \'bar\';514-};515-.516-.fi517-.518-.IP "" 0519-.520-.P521-Which we could then utilize in a template as shown below:522-.523-.IP "" 4524-.525-.nf526-527-<%= foo() %>528-.529-.fi530-.531-.IP "" 0532-.504 .SS "Template Engines"505 Below are a few template engines commonly used with Express:506 .507 .IP "\(bu" 4468 unmodified lines976 .fi977 .978 .IP "" 0979 .980+.SS "app\.helpers(obj)"981+Registers both static and dynamic view helpers\. Dynamic view helpers are simply functions which accept \fIreq\fR, \fIres\fR, and \fIparams\fR, and are evaluated against the \fIServer\fR instance\. The \fIreturn value\fR of this function becomes the local variable it is associated with\.982+.983+.IP "" 4984+.985+.nf986+987+app\.helpers({988+ siteTitle: \'My Site\',989+ session: function(req, res, params){990+ return req\.session;991+ }992+});993+.994+.fi995+.996+.IP "" 0997+.998+.P999+All views would now have \fIsiteTitle\fR available, and \fIsession\fR available so that session data could be accessed via \fIsession\.name\fR etc\.1000+.1001 .SS "app\.listen([port[, host]])"1002 Bind the app server to the given \fIport\fR, which defaults to 3000\. When \fIhost\fR is omitted all connections will be accepted via \fIINADDR_ANY\fR\.1003 .1004 .IP "" 444 unmodified linesmodified:docs/guide.html+19 added-17 removed
174 unmodified lines175 <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="#View-Helpers">View Helpers</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>13 unmodified lines196 <li><a href="#app-disable-name-">app.disable()</a></li>197 <li><a href="#app-configure-env-function-function-">app.configure()</a></li>198 <li><a href="#app-redirect-name-val-">app.redirect()</a></li>199 <li><a href="#app-error-function-">app.error()</a></li>200+<li><a href="#app-helpers-obj-">app.helpers()</a></li>201 <li><a href="#app-listen-port-host-">app.listen()</a></li>202 </ul>203 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 204 <p id="tagline">328 unmodified lines533 <li>_lastInCollection _ True if this is the last object</li>534 </ul>535 536 537-<h3 id="View-Helpers">View Helpers</h3>538-539-<p>View helpers are merged with all template local variables, exposing them540-as pseudo globals. Below is an example of exposing <em>foo()</em> to all views:</p>541-542-<pre><code>var view = require('express/view');543-view.helpers.foo = function(){544- return 'bar';545-};546-</code></pre>547-548-<p>Which we could then utilize in a template as shown below:</p>549-550-<pre><code><%= foo() %>551-</code></pre>552-537 <h3 id="Template-Engines">Template Engines</h3>538 539 <p>Below are a few template engines commonly used with Express:</p>540 313 unmodified lines854 res.send(err.message, 500);855 });856 </code></pre>857 858+<h3 id="app-helpers-obj-">app.helpers(obj)</h3>859+860+<p>Registers both static and dynamic view helpers. Dynamic view helpers861+are simply functions which accept <em>req</em>, <em>res</em>, and <em>params</em>, and are862+evaluated against the <em>Server</em> instance. The <em>return value</em> of this function863+becomes the local variable it is associated with.</p>864+865+<pre><code>app.helpers({866+ siteTitle: 'My Site',867+ session: function(req, res, params){868+ return req.session;869+ }870+});871+</code></pre>872+873+<p>All views would now have <em>siteTitle</em> available, and <em>session</em> available874+so that session data could be accessed via <em>session.name</em> etc.</p>875+876 <h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>877 878 <p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all879 connections will be accepted via <em>INADDR_ANY</em>.</p>28 unmodified linesmodified:docs/guide.md+17 added-14 removed
286 unmodified lines287 * _firstInCollection_ True if this is the first object288 * _indexInCollection_ Index of the object in the collection289 * _lastInCollection _ True if this is the last object290 291-### View Helpers292-293-View helpers are merged with all template local variables, exposing them294-as pseudo globals. Below is an example of exposing _foo()_ to all views:295-296- var view = require('express/view');297- view.helpers.foo = function(){298- return 'bar';299- };300-301-Which we could then utilize in a template as shown below:302-303- <%= foo() %>304-291 ### Template Engines292 293 Below are a few template engines commonly used with Express:294 274 unmodified lines569 app.error(function(err, req, res, next){570 res.send(err.message, 500);571 });572 573+### app.helpers(obj)574+575+Registers both static and dynamic view helpers. Dynamic view helpers576+are simply functions which accept _req_, _res_, and _params_, and are577+evaluated against the _Server_ instance. The _return value_ of this function578+becomes the local variable it is associated with.579+580+ app.helpers({581+ siteTitle: 'My Site',582+ session: function(req, res, params){583+ return req.session;584+ }585+ });586+587+All views would now have _siteTitle_ available, and _session_ available588+so that session data could be accessed via _session.name_ etc.589+590 ### app.listen([port[, host]])591 592 Bind the app server to the given _port_, which defaults to 3000. When _host_ is omitted all593 connections will be accepted via _INADDR_ANY_.19 unmodified linesmodified:docs/index.1+3 added
26 unmodified lines27 .IP "\(bu" 428 Redirection helpers29 .30 .IP "\(bu" 431+Dynamic view helpers32+.33+.IP "\(bu" 434 Content negotiation35 .36 .IP "\(bu" 437 Focus on high performance57 unmodified linesmodified:docs/index.html+1 added
190 unmodified lines191 192 <ul>193 <li>Robust routing</li>194 <li>Redirection helpers</li>195+<li>Dynamic view helpers</li>196 <li>Content negotiation</li>197 <li>Focus on high performance</li>198 <li>View rendering and partials support</li>199 <li>Environment based configuration</li>35 unmodified linesmodified:docs/index.md+1 added
9 unmodified lines10111213141516171821 unmodified lines9 unmodified lines## Features
* Robust routing * Redirection helpers * Dynamic view helpers * Content negotiation * Focus on high performance * View rendering and partials support * Environment based configuration21 unmodified linesmodified:Readme.md+1 added
30 unmodified lines31323334353637383963 unmodified lines30 unmodified lines## Features
* Robust routing * Redirection helpers * Dynamic view helpers * Content negotiation * Focus on high performance * View rendering and partials support * Environment based configuration63 unmodified lines