Merge branch 'app-level-view-options'
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 · parents 01b3caa, 4d98552 · Browse files · 7 files·+65 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+65 added−2 removed
- tests:+39 added−0 removed
- other:+26 added−2 removed
Provenance
- Commit
706e32c- Tree
b14ab8c- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
01b3caa- Parent 2
4d98552
7 files changed+65 added−2 removed
modified:docs/guide.md+18 added
81 unmodified lines82 * _env_ Application environment set internally, use _app.set('env')_ on _Server#listen()_83 * _home_ Application base path used for _res.redirect()_ and transparently handling mounted apps.84 * _views_ Root views directory defaulting to **CWD/views**85 * _view engine_ Default view engine name for views rendered without extensions86+ * _view options_ An object specifying global view options87 * _partials_ Root view partials directory defaulting to _views_/partials. 88 89 ### Routing90 190 unmodified lines281 mix and match template engines:282 283 res.render('another-page.ejs');284 285+Express also provides the _view options_ setting, which is applied each time a view is rendered, so for example if you rarely use layouts you may set:286+287+ app.set('view options', {288+ layout: false289+ });290+291+Which can then be overridden within the `res.render()` call if need be:292+293+ res.render('myview.ejs', { layout: true });294+295+A good example of this is specifying custom _ejs_ opening and closing tags:296+297+ app.set('view options', {298+ open: '{{',299+ close: '}}'300+ });301+302 ### View Partials303 304 The Express view system has built-in support for partials and collections, which are305 sort of "mini" views representing a document fragment. For example rather than iterating371 unmodified linesmodified:docs/index.md+1 added
10 unmodified lines11121314151617181933 unmodified lines10 unmodified lines
* Robust routing * Redirection helpers * Dynamic view helpers * Application level view options * Content negotiation * Focus on high performance * View rendering and partials support * Environment based configuration33 unmodified linesmodified:lib/express/view.js+6 added-2 removed
153 unmodified lines154 * @api public155 */156 157 http.ServerResponse.prototype.render = function(view, options, fn){158- options = options || {};159- var defaultEngine = this.app.set('view engine');158+ var options = options || {},159+ viewOptions = this.app.set('view options'),160+ defaultEngine = this.app.set('view engine');161 162+ // Mixin "view options"163+ if (viewOptions) options.__proto__ = viewOptions;164+165 // Support "view engine" setting166 if (view.indexOf('.') < 0 && defaultEngine) {167 view += '.' + defaultEngine;168 }103 unmodified linesadded:test/fixtures/user.ejs+2 added
1+<h1>{{= name }}</h1>2+<p>{{= email }}</p>added:test/fixtures/video.ejs+1 added
1+<h1><?= title ?></h1>modified:test/view.test.js+36 added
363 unmodified lines364 365 assert.response(app,366 { url: '/' },367 { body: '<ul><li class="item">foo</li><li class="item">bar</li></ul>' });368+ },369+ 370+ 'test "view options"': function(assert){371+ var app = create();372+ 373+ app.set('view options', {374+ layout: false,375+ open: '{{',376+ close: '}}'377+ });378+ 379+ app.get('/', function(req, res, next){380+ res.render('user.ejs', {381+ locals: {382+ name: 'tj',383+ email: 'tj@vision-media.ca'384+ }385+ });386+ });387+ 388+ app.get('/video', function(req, res, next){389+ res.render('video.ejs', {390+ open: '<?',391+ close: '?>',392+ locals: {393+ title: 'keyboard cat'394+ }395+ });396+ });397+ 398+ assert.response(app,399+ { url: '/' },400+ { body: '<h1>tj</h1>\n<p>tj@vision-media.ca</p>' });401+ assert.response(app,402+ { url: '/video' },403+ { body: '<h1>keyboard cat</h1>' });404 }405 };modified:Readme.md+1 added
39 unmodified lines40414243444546474857 unmodified lines39 unmodified lines * Session based flash notifications * Built on [Connect](http://github.com/senchalabs/connect) * High test coverage * Executable for generating applications quickly * Application level view options
Via Connect:
* Session support57 unmodified lines