Skip to content
Myr

Docs for Server#{helpers,dynamicHelpers}()

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 69fb9bb · Browse files · 4 files+106 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+106 added−25 removed

  • other:+106 added−25 removed

4 files+106 added−25 removed

modified:docs/api.html+18 added-10 removed
588 unmodified lines
589<pre><code><span class="keyword">var</span> <span class="variable">sys</span> = <span class="variable">require</span>(<span class="string">'sys'</span>),590 <span class="variable">url</span> = <span class="variable">require</span>(<span class="string">'url'</span>),591 <span class="variable">view</span> = <span class="variable">require</span>(<span class="string">'./view'</span>),592 <span class="variable">connect</span> = <span class="variable">require</span>(<span class="string">'connect'</span>),593 <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),594 <span class="variable">queryString</span> = <span class="variable">require</span>(<span class="string">'querystring'</span>),595 <span class="variable">router</span> = <span class="variable">require</span>(<span class="string">'connect/middleware/router'</span>);</code></pre>596</td>597</tr>
76 unmodified lines
674<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>675</td>676<td class="code">677<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 }678 <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="variable">view</span>.<span class="variable">helpers</span>, <span class="variable">obj</span>);679 <span class="keyword">return</span> <span class="this">this</span>;680};</code></pre>681</td>682</tr>683<tr class="code">684<td class="docs">685<p>Register the given dynamic view helpers <code>obj</code>. This method686can be called several times to apply additional helpers.</p>687688<h2></h2>689690<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>691</td>692<td class="code">693<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">dynamicHelpers</span> = <span class="keyword">function</span>(<span class="variable">obj</span>){694 <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="variable">view</span>.<span class="variable">dynamicHelpers</span>, <span class="variable">obj</span>);695 <span class="keyword">return</span> <span class="this">this</span>;696};</code></pre>697</td>698</tr>699<tr class="code">700<td class="docs">701<p>Assign a custom exception handler callback <code>fn</code>.</p>702703<h2></h2>704
403 unmodified lines
modified:docs/guide.1+42 added-3 removed
976 unmodified lines
977.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\.981Registers static view helpers\.982.983.IP "" 4984.985.nf986987app\.helpers({988 siteTitle: \'My Site\',988 name: function(first, last){ return first + \', \' + last },989 firstName: \'tj\',990 lastName: \'holowaychuk\'991});992.993.fi994.995.IP "" 0996.997.P998Our view could now utilize the \fIfirstName\fR and \fIlastName\fR variables, as well as the \fIname()\fR function exposed\.999.1000.IP "" 41001.1002.nf10031004<%= name(firstName, lastName) %>1005.1006.fi1007.1008.IP "" 01009.1010.SS "app\.dynamicHelpers(obj)"1011Registers 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 before a view is rendered\. The \fIreturn value\fR of this function becomes the local variable it is associated with\.1012.1013.IP "" 41014.1015.nf10161017app\.dynamicHelpers({1018 session: function(req, res, params){1019 return req\.session;1020 }1021});
2 unmodified lines
1024.1025.IP "" 01026.1027.P999All views would now have \fIsiteTitle\fR available, and \fIsession\fR available so that session data could be accessed via \fIsession\.name\fR etc\.1028All views would now have \fIsession\fR available so that session data can be accessed via \fIsession\.name\fR etc:1029.1030.IP "" 41031.1032.nf10331034<%= session\.name %>1035.1036.fi1037.1038.IP "" 01039.1040.SS "app\.listen([port[, host]])"1041Bind 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\.1042.1043.IP "" 4
44 unmodified lines
modified:docs/guide.html+25 added-6 removed
196 unmodified lines
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-dynamicHelpers-obj-">app.dynamicHelpers()</a></li>202<li><a href="#app-listen-port-host-">app.listen()</a></li>203</ul>204 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 205 <p id="tagline">
651 unmodified lines
857</code></pre>858859<h3 id="app-helpers-obj-">app.helpers(obj)</h3>860861<p>Registers static view helpers.</p>862863<pre><code>app.helpers({864 name: function(first, last){ return first + ', ' + last },865 firstName: 'tj',866 lastName: 'holowaychuk'867});868</code></pre>869870<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,871as well as the <em>name()</em> function exposed.</p>872873<pre><code>&lt;%= name(firstName, lastName) %>874</code></pre>875876<h3 id="app-dynamicHelpers-obj-">app.dynamicHelpers(obj)</h3>877860<p>Registers both static and dynamic view helpers. Dynamic view helpers878<p>Registers dynamic view helpers. Dynamic view helpers879are 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 function880evaluated against the <em>Server</em> instance before a view is rendered. The <em>return value</em> of this function881becomes the local variable it is associated with.</p>882865<pre><code>app.helpers({866 siteTitle: 'My Site',883<pre><code>app.dynamicHelpers({884 session: function(req, res, params){885 return req.session;886 }887});888</code></pre>889873<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>890<p>All views would now have <em>session</em> available so that session data can be accessed via <em>session.name</em> etc:</p>891892<pre><code>&lt;%= session.name %&gt;893</code></pre>894895<h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>896897<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all898connections will be accepted via <em>INADDR_ANY</em>.</p>
28 unmodified lines
modified:docs/guide.md+21 added-6 removed
570 unmodified lines
571 });572573### app.helpers(obj)574575Registers static view helpers.576577 app.helpers({578 name: function(first, last){ return first + ', ' + last },579 firstName: 'tj',580 lastName: 'holowaychuk'581 });582583Our view could now utilize the _firstName_ and _lastName_ variables,584as well as the _name()_ function exposed.585586 <%= name(firstName, lastName) %>587588### app.dynamicHelpers(obj)589575Registers both static and dynamic view helpers. Dynamic view helpers590Registers dynamic view helpers. Dynamic view helpers591are simply functions which accept _req_, _res_, and _params_, and are577evaluated against the _Server_ instance. The _return value_ of this function592evaluated against the _Server_ instance before a view is rendered. The _return value_ of this function593becomes the local variable it is associated with.594580 app.helpers({581 siteTitle: 'My Site',595 app.dynamicHelpers({596 session: function(req, res, params){597 return req.session;598 }599 });600587All views would now have _siteTitle_ available, and _session_ available588so that session data could be accessed via _session.name_ etc.601All views would now have _session_ available so that session data can be accessed via _session.name_ etc:602603 <%= session.name %>604605### app.listen([port[, host]])606607Bind the app server to the given _port_, which defaults to 3000. When _host_ is omitted all608connections will be accepted via _INADDR_ANY_.
19 unmodified lines