Added extending-templates example. Closes #730
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 09c9452 · Browse files · 4 files·+86 added−0 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+86 added−0 removed
- other:+86 added−0 removed
Provenance
- Commit
dbbe7be- Tree
4319508- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
09c9452
4 files changed+86 added−0 removed
added:examples/extending-templates/views/users/index.html+8 added
12345678<% page.title = 'Users' %><% script('/javascripts/jquery.js') %><% script('/javascripts/users.js') %>
<h1>Users</h1><ul id="users"> <%- partial('user', users) %></ul>No newline at end of fileadded:examples/extending-templates/views/users/user.html+1 added
1<li><%= user.name %> <<%= user.email %>></li>No newline at end of fileadded:examples/extending-templates/views/layout.html+11 added
1234567891011<html> <head> <title><%- page.title %></title> <% for (var i in scripts) { %> <script src="<%= scripts[i] %>"></script> <% } %> </head> <body> <%- body %> </body></html>No newline at end of fileadded:examples/extending-templates/app.js+66 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
/** * Module dependencies. */
var express = require('../../lib/express');
var app = express.createServer();
// Register ejs as .html
app.register('.html', require('ejs'));
// Optional since express defaults to CWD/views
app.set('views', __dirname + '/views');app.set('view engine', 'html');
// Dummy usersvar users = [ { name: 'tj', email: 'tj@sencha.com' } , { name: 'ciaran', email: 'ciaranj@gmail.com' } , { name: 'aaron', email: 'aaron.heckmann+github@gmail.com' }];
// dynamic helpers are simply functions that are invoked// per request (once), passed both the request and response// objects. These can be used for request-specific// details within a view, such telling the layout which// scripts to include.app.dynamicHelpers({ // by simply returning an object here // we can set it's properties such as "page.title" // within a view, and it remains specific to that request, // so it would be valid to do: // page.title = user.name + "'s account" page: function() { return {}; },
// the scripts array here is assigned once, // so by returning a closure, we can use script(path) // in a template, instead of something like // scripts.push(path). script: function(req){ req._scripts = []; return function(path){ req._scripts.push(path); } },
// to expose our scripts array for iteration within // our views (typically the layout), we simply return it // here, and since composite types are mutable, it will // contain all of the paths pushed with the helper above. scripts: function(req){ return req._scripts; }});
app.get('/', function(req, res){ res.render('users', { users: users });});
app.listen(3000);console.log('Express app started on port 3000');No newline at end of file