Skip to content
Myr

Added view-locals example

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 6e26a8d · Browse files · 4 files+183 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+183 added−0 removed

  • other:+183 added−0 removed

4 files+183 added−0 removed

added:examples/view-locals/index.js+131 added
12var express = require('../..')3 , User = require('./user')4 , app = express();56app.set('views', __dirname);7app.set('view engine', 'jade');89// filter ferrets only1011function ferrets(user) {12 return user.species == 'ferret';13}1415// naive nesting approach,16// delegating errors to next(err)17// in order to expose the "count"18// and "users" locals1920app.get('/', function(req, res, next){21 User.count(function(err, count){22 if (err) return next(err);23 User.all(function(err, users){24 if (err) return next(err);25 res.render('user', {26 title: 'Users',27 count: count,28 users: users.filter(ferrets)29 });30 })31 })32});3334353637// this approach is cleaner,38// less nesting and we have39// the variables available40// on the request object4142function count(req, res, next) {43 User.count(function(err, count){44 if (err) return next(err);45 req.count = count;46 next();47 })48}4950function users(req, res, next) {51 User.all(function(err, users){52 if (err) return next(err);53 req.users = users;54 next();55 })56}5758app.get('/middleware', count, users, function(req, res, next){59 res.render('user', {60 title: 'Users',61 count: req.count,62 users: req.users.filter(ferrets)63 });64});6566676869// this approach is much like the last70// however we're explicitly exposing71// the locals within each middleware72// 73// note that this may not always work74// well, for example here we filter75// the users in the middleware, which76// may not be ideal for our application.77// so in that sense the previous example78// is more flexible with `req.users`.7980function count2(req, res, next) {81 User.count(function(err, count){82 if (err) return next(err);83 res.locals.count = count;84 next();85 })86}8788function users2(req, res, next) {89 User.all(function(err, users){90 if (err) return next(err);91 // this would not be ideal for *this*92 res.locals.users = users.filter(ferrets);93 next();94 })95}9697app.get('/middleware-locals', count2, users2, function(req, res, next){98 // you can see now how we have much less99 // to pass to res.render(). If we have100 // several routes related to users this101 // can be a great productivity booster102 res.render('user', { title: 'Users' });103});104105106// let's assume we wanted to load the users107// and count for every res.render() call, we108// could use app.locals.use() for this. These109// are callbacks which run in parallel ONLY110// when res.render() is invoked. If no views111// are rendered, there is no overhead.112113// This may be ideal if you want to load auxiliary114// user information, but only for templates. Note115// that (req, res) are available to you, so you may116// access req.session.user etc.117118// Keep in mind these execute in *parallel*, so these119// callbacks should not depend on each other, this120// also makes them slightly more efficient than121// using middleware which execute sequentially122123app.locals.use(count2);124app.locals.use(users2);125126app.get('/locals', function(req, res){127 res.render('user', { title: 'Users' });128});129130app.listen(3000);131console.log('Application listening on port 3000');
added:examples/view-locals/layout.jade+12 added
1doctype 52html3 head4 title= title5 style6 body {7 padding: 50px;8 font: 16px Helvetica, Arial;9 }10 body11 h2= title12 block content
added:examples/view-locals/user.jade+8 added
12extends layout34block content5 for user in users6 .user7 h3= user.name8 p #{user.name} is a #{user.age} year old #{user.species}.
added:examples/view-locals/user.js+32 added