Skip to content
Myr

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

8 files+113 added−60 removed

modified:docs/api.html+50 added
663 unmodified lines
664</td>665</tr>666<tr class="code">667<td class="docs">668<p>Register the given view helpers <code>obj</code>. This method669can be called several times to apply additional helpers.</p>670671<h2></h2>672673<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>694695<h2></h2>696
199 unmodified lines
896</td>897</tr>898<tr class="code">899<td class="docs">900<p>Dynamic view helper functions.</p>901902<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>911912<h2>Options</h2>913
88 unmodified lines
1002 <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>;10051006 <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 }10101011 <span class="comment">// Normalize &quot;context&quot; to &quot;scope&quot;</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>;10131014 <span class="comment">// Auto-cache in production</span>
21 unmodified lines
1036 <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>;10391040 <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 }10491050 <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 lines
modified:docs/guide.1+21 added-29 removed
499 unmodified lines
500_lastInCollection _ True if this is the last object501.502.IP "" 0503.504.SS "View Helpers"505View 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.nf510511var view = require(\'express/view\');512view\.helpers\.foo = function(){513 return \'bar\';514};515.516.fi517.518.IP "" 0519.520.P521Which we could then utilize in a template as shown below:522.523.IP "" 4524.525.nf526527<%= foo() %>528.529.fi530.531.IP "" 0532.504.SS "Template Engines"505Below are a few template engines commonly used with Express:506.507.IP "\(bu" 4
468 unmodified lines
976.fi977.978.IP "" 0979.980.SS "app\.helpers(obj)"981Registers 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.nf986987app\.helpers({988 siteTitle: \'My Site\',989 session: function(req, res, params){990 return req\.session;991 }992});993.994.fi995.996.IP "" 0997.998.P999All 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]])"1002Bind 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 "" 4
44 unmodified lines
modified:docs/guide.html+19 added-17 removed
174 unmodified lines
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="#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 lines
196<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 lines
533<li>_lastInCollection _ True if this is the last object</li>534</ul>535536537<h3 id="View-Helpers">View Helpers</h3>538539<p>View helpers are merged with all template local variables, exposing them540as pseudo globals. Below is an example of exposing <em>foo()</em> to all views:</p>541542<pre><code>var view = require('express/view');543view.helpers.foo = function(){544 return 'bar';545};546</code></pre>547548<p>Which we could then utilize in a template as shown below:</p>549550<pre><code>&lt;%= foo() %>551</code></pre>552537<h3 id="Template-Engines">Template Engines</h3>538539<p>Below are a few template engines commonly used with Express:</p>540
313 unmodified lines
854 res.send(err.message, 500);855});856</code></pre>857858<h3 id="app-helpers-obj-">app.helpers(obj)</h3>859860<p>Registers both static and dynamic view helpers. Dynamic view helpers861are simply functions which accept <em>req</em>, <em>res</em>, and <em>params</em>, and are862evaluated against the <em>Server</em> instance. The <em>return value</em> of this function863becomes the local variable it is associated with.</p>864865<pre><code>app.helpers({866 siteTitle: 'My Site',867 session: function(req, res, params){868 return req.session;869 }870});871</code></pre>872873<p>All views would now have <em>siteTitle</em> available, and <em>session</em> available874so that session data could be accessed via <em>session.name</em> etc.</p>875876<h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>877878<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all879connections will be accepted via <em>INADDR_ANY</em>.</p>
28 unmodified lines
modified:docs/guide.md+17 added-14 removed
286 unmodified lines
287 * _firstInCollection_ True if this is the first object288 * _indexInCollection_ Index of the object in the collection289 * _lastInCollection _ True if this is the last object290291### View Helpers292293View helpers are merged with all template local variables, exposing them294as pseudo globals. Below is an example of exposing _foo()_ to all views:295296 var view = require('express/view');297 view.helpers.foo = function(){298 return 'bar';299 };300301Which we could then utilize in a template as shown below:302303 <%= foo() %>304291### Template Engines292293Below are a few template engines commonly used with Express:294
274 unmodified lines
569 app.error(function(err, req, res, next){570 res.send(err.message, 500);571 });572573### app.helpers(obj)574575Registers both static and dynamic view helpers. Dynamic view helpers576are simply functions which accept _req_, _res_, and _params_, and are577evaluated against the _Server_ instance. The _return value_ of this function578becomes the local variable it is associated with.579580 app.helpers({581 siteTitle: 'My Site',582 session: function(req, res, params){583 return req.session;584 }585 });586587All views would now have _siteTitle_ available, and _session_ available588so that session data could be accessed via _session.name_ etc.589590### app.listen([port[, host]])591592Bind the app server to the given _port_, which defaults to 3000. When _host_ is omitted all593connections will be accepted via _INADDR_ANY_.
19 unmodified lines
modified:docs/index.1+3 added
26 unmodified lines
27.IP "\(bu" 428Redirection helpers29.30.IP "\(bu" 431Dynamic view helpers32.33.IP "\(bu" 434Content negotiation35.36.IP "\(bu" 437Focus on high performance
57 unmodified lines
modified:docs/index.html+1 added
190 unmodified lines
191192<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 lines
modified:docs/index.md+1 added
modified:Readme.md+1 added