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
Provenance
- Commit
78008d1- Tree
a265c3f- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
69fb9bb
4 files changed+106 added−25 removed
modified:docs/api.html+18 added-10 removed
588 unmodified lines589 <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 lines674 <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 method686+can be called several times to apply additional helpers.</p>687+688+<h2></h2>689+690+<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>702 703 <h2></h2>704 403 unmodified linesmodified:docs/guide.1+42 added-3 removed
976 unmodified lines977 .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\.981+Registers static view helpers\.982 .983 .IP "" 4984 .985 .nf986 987 app\.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+.P998+Our view could now utilize the \fIfirstName\fR and \fIlastName\fR variables, as well as the \fIname()\fR function exposed\.999+.1000+.IP "" 41001+.1002+.nf1003+1004+<%= name(firstName, lastName) %>1005+.1006+.fi1007+.1008+.IP "" 01009+.1010+.SS "app\.dynamicHelpers(obj)"1011+Registers 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+.nf1016+1017+app\.dynamicHelpers({1018 session: function(req, res, params){1019 return req\.session;1020 }1021 });2 unmodified lines1024 .1025 .IP "" 01026 .1027 .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\.1028+All views would now have \fIsession\fR available so that session data can be accessed via \fIsession\.name\fR etc:1029 .1030+.IP "" 41031+.1032+.nf1033+1034+<%= session\.name %>1035+.1036+.fi1037+.1038+.IP "" 01039+.1040 .SS "app\.listen([port[, host]])"1041 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\.1042 .1043 .IP "" 444 unmodified linesmodified:docs/guide.html+25 added-6 removed
196 unmodified lines197 <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 lines857 </code></pre>858 859 <h3 id="app-helpers-obj-">app.helpers(obj)</h3>860 861+<p>Registers static view helpers.</p>862+863+<pre><code>app.helpers({864+ name: function(first, last){ return first + ', ' + last },865+ firstName: 'tj',866+ lastName: 'holowaychuk'867+});868+</code></pre>869+870+<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,871+as well as the <em>name()</em> function exposed.</p>872+873+<pre><code><%= name(firstName, lastName) %>874+</code></pre>875+876+<h3 id="app-dynamicHelpers-obj-">app.dynamicHelpers(obj)</h3>877+860-<p>Registers both static and dynamic view helpers. Dynamic view helpers878+<p>Registers dynamic view helpers. Dynamic view helpers879 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 function880+evaluated against the <em>Server</em> instance before a view is rendered. The <em>return value</em> of this function881 becomes the local variable it is associated with.</p>882 865-<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>889 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>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>891 892+<pre><code><%= session.name %>893+</code></pre>894+895 <h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>896 897 <p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all898 connections will be accepted via <em>INADDR_ANY</em>.</p>28 unmodified linesmodified:docs/guide.md+21 added-6 removed
570 unmodified lines571 });572 573 ### app.helpers(obj)574 575+Registers static view helpers.576+577+ app.helpers({578+ name: function(first, last){ return first + ', ' + last },579+ firstName: 'tj',580+ lastName: 'holowaychuk'581+ });582+583+Our view could now utilize the _firstName_ and _lastName_ variables,584+as well as the _name()_ function exposed.585+586+ <%= name(firstName, lastName) %>587+588+### app.dynamicHelpers(obj)589+575-Registers both static and dynamic view helpers. Dynamic view helpers590+Registers dynamic view helpers. Dynamic view helpers591 are simply functions which accept _req_, _res_, and _params_, and are577-evaluated against the _Server_ instance. The _return value_ of this function592+evaluated against the _Server_ instance before a view is rendered. The _return value_ of this function593 becomes the local variable it is associated with.594 580- app.helpers({581- siteTitle: 'My Site',595+ app.dynamicHelpers({596 session: function(req, res, params){597 return req.session;598 }599 });600 587-All views would now have _siteTitle_ available, and _session_ available588-so that session data could be accessed via _session.name_ etc.601+All views would now have _session_ available so that session data can be accessed via _session.name_ etc:602 603+ <%= session.name %>604+605 ### app.listen([port[, host]])606 607 Bind the app server to the given _port_, which defaults to 3000. When _host_ is omitted all608 connections will be accepted via _INADDR_ANY_.19 unmodified lines