Moved render() to Request
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 0f3a764 · Browse files · 2 files·+66 added−69 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+66 added−69 removed
- other:+66 added−69 removed
Provenance
- Commit
e182388- Tree
f8e2aea- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
0f3a764
2 files changed+66 added−69 removed
modified:examples/app.js+11 added-20 removed
20 unmodified lines2122232425252627282930313233333435363738394041424344454647484950515253544243444546474849505651525359606162545556575820 unmodified lines this.redirect('/chat')})
get('/chat', function(){ render('chat.haml.html', { this.render('chat.haml.html', { locals: { messages: messages } })})
post('/chat', function(){ messages.push(param('message')) messages.push(this.param('message')) this.halt(200)})
get('/chat/messages', function(){ this.contentType('json') return JSON.encode(messages)})
StaticFile = Class({ init: function(path) { this.path = path }, send: function() { var self = this path.exists(this.path, function(exists){ if (!exists) halt() posix.cat(self.path).addCallback(function(content){ contentType(self.path) halt(200, content) })get('/public/:file', function(file){ var self = this file = dirname(__filename) + '/public/' + file path.exists(file, function(exists){ if (!exists) self.halt() posix.cat(file).addCallback(function(content){ self.contentType(file) self.halt(200, content) }) } })})
get('/public/:file', function(file){ (new StaticFile(dirname(__filename) + '/public/' + file)).send()})
get('/error', function(){ throw new Error('oh noes!')})
run()modified:lib/express/view.js+55 added-49 removed
16 unmodified lines17181920212223242526272829303132333435363738394041424321222324252627282930313233343536373839404142434445464748494546474849505152535455565758596050515253545556575859606162636465666768697071627273747576776465666768697071787216 unmodified lines ejs: require('support/ejs'), haml: require('support/haml')}
/** * Render _view_ with _options_. * * Views are looked up relative to the'views' path setting. * View filenames should conform to ANY.ENGINE.TYPE so for example * 'layout.ejs.html', 'ejs' represents the template engine, 'html' * represents the type of content being rendered, which is then passed * to contentType(). * * Engines must export a render() method accepting the template string * and a hash of options. * * Options: * * - layout: Whether or not to use a layout. Defaults to true * - context: Most engines support an evaluation context (the 'this' keyword) * - locals: Most engines support a hash of local variable names / values. * * @param {string} view * @param {hash} options * @settings 'views', 'cache views' * @api public */// --- Request
Request.include({ /** * Render _view_ with _options_. * * Views are looked up relative to the'views' path setting. * View filenames should conform to ANY.ENGINE.TYPE so for example * 'layout.ejs.html', 'ejs' represents the template engine, 'html' * represents the type of content being rendered, which is then passed * to contentType(). * * Engines must export a render() method accepting the template string * and a hash of options. * * Options: * * - layout: Whether or not to use a layout. Defaults to true * - context: Most engines support an evaluation context (the 'this' keyword) * - locals: Most engines support a hash of local variable names / values. * * @param {string} view * @param {hash} options * @settings 'views', 'cache views' * @api public */
render: function(view, options) {var render = exports.render = function(view, options) { options = options || {} var path = set('views') + '/' + view, type = path.split('.').slice(-2)[0], ext = extname(path), layout = options.layout === undefined ? true : options.layout contentType(ext) function go(content) { content = engine[type].render(content, options) if (layout) render('layout.' + type + '.' + ext, process.mixin(options, { layout: false, locals: { body: content } })) var self = this, options = options || {}, path = set('views') + '/' + view, type = path.split('.').slice(-2)[0], ext = extname(path), layout = options.layout === undefined ? true : options.layout self.contentType(ext) function render(content) { content = engine[type].render(content, options) if (layout) self.render('layout.' + type + '.' + ext, process.mixin(options, { layout: false, locals: { body: content } })) else self.halt(200, content) } if (set('cache views') && cache[view]) render(cache[view]) else halt(200, content) posix .cat(path) .addCallback(function(content){ render(cache[view] = content) }) } if (set('cache views') && cache[view]) go(cache[view]) else posix .cat(path) .addCallback(function(content){ go(cache[view] = content) })})}No newline at end of file