View is now a plugin [#87]
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent a52845e · Browse files · 6 files·+114 added−98 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+114 added−98 removed
- tests:+16 added−18 removed
- other:+98 added−80 removed
Provenance
- Commit
8e408e3- Tree
5874fbf- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
a52845e
6 files changed+114 added−98 removed
added:lib/express/plugins/view.js+97 added
1+2+// Express - View - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3+4+var posix = require('posix')5+6+/**7+ * Template content cache.8+ */9+10+var cache = {}11+12+/**13+ * Support template engines.14+ */15+16+var engine = {17+ ejs: require('support/ejs'),18+ haml: require('support/haml')19+}20+21+// --- View22+23+exports.View = Plugin.extend({24+ extend: {25+ 26+ /**27+ * Initialize extensions.28+ */29+ 30+ init: function() {31+ 32+ // Settings33+ 34+ set('views', function(){ return set('root') + '/views' })35+ 36+ // Request37+ 38+ Request.include({39+40+ /**41+ * Render _view_ with _options_.42+ *43+ * Views are looked up relative to the'views' path setting. 44+ * View filenames should conform to ANY.ENGINE.TYPE so for example45+ * 'layout.ejs.html', 'ejs' represents the template engine, 'html'46+ * represents the type of content being rendered, which is then passed47+ * to contentType().48+ *49+ * Engines must export a render() method accepting the template string50+ * and a hash of options.51+ *52+ * Options:53+ *54+ * - layout: Whether or not to use a layout. Defaults to true55+ * - context: Most engines support an evaluation context (the 'this' keyword)56+ * - locals: Most engines support a hash of local variable names / values.57+ *58+ * @param {string} view59+ * @param {hash} options60+ * @settings 'views', 'cache views'61+ * @api public62+ */63+64+ render: function(view, options) {65+ var self = this,66+ options = options || {},67+ path = set('views') + '/' + view,68+ type = path.split('.').slice(-2)[0],69+ ext = extname(path),70+ layout = options.layout === undefined ? true : options.layout71+ self.contentType(ext)72+ function render(content) {73+ content = engine[type].render(content, options)74+ if (layout)75+ self.render('layout.' + type + '.' + ext, process.mixin(options, {76+ layout: false,77+ locals: {78+ body: content79+ }80+ }))81+ else82+ self.halt(200, content)83+ }84+ if (set('cache views') && cache[view])85+ render(cache[view])86+ else87+ posix88+ .cat(path)89+ .addCallback(function(content){90+ render(cache[view] = content)91+ })92+ }93+ })94+ }95+ }96+})97+modified:lib/express/core.js+1 added-2 removed
7 unmodified lines8 process.mixin(require('express/request'))9 process.mixin(require('express/helpers'))10 process.mixin(require('express/plugin'))11 process.mixin(require('express/mime'))12-process.mixin(require('express/view'))12 process.mixin(require('express/dsl'))13 14 // --- Route15 238 unmodified lines254 255 // --- Defaults256 257 configure(function(){259- set('views', function(){ return set('root') + '/views' })258+ use(require('express/plugins/view').View)259 use(require('express/plugins/body-decoder').BodyDecoder)260 })261 262 configure('development', function(){7 unmodified linesdeleted:lib/express/view.js-78 removed
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
// Express - View - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
var posix = require('posix')
/** * Template content cache. */
var cache = {}
/** * Support template engines. */
var engine = { ejs: require('support/ejs'), haml: require('support/haml')}
// --- 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 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 posix .cat(path) .addCallback(function(content){ render(cache[view] = content) }) }})No newline at end of filemodified:spec/spec.node.js+1 added-1 removed
25 unmodified lines262728293030313233343536375 unmodified lines25 unmodified lines .exec('spec/spec.routing.js') .exec('spec/spec.helpers.js') .exec('spec/spec.request.js') .exec('spec/spec.mime.js') .exec('spec/spec.view.js') .exec('spec/spec.collection.js') .exec('spec/spec.element-collection.js') .exec('spec/spec.plugins.js') .exec('spec/spec.plugins.view.js') .exec('spec/spec.plugins.common-logger.js') .exec('spec/spec.plugins.content-length.js') .exec('spec/spec.plugins.method-override.js') .exec('spec/spec.plugins.body-decoder.js')5 unmodified linesadded:spec/spec.plugins.view.js+15 added
123456789101112131415
describe 'Express' before_each reset() end describe 'View' describe 'set("views")' it 'should default to <root>/views' set('root', 'spec') set('views').should.eql 'spec/views' end end endendNo newline at end of filedeleted:spec/spec.view.js-17 removed
1234567891011121314151617
describe 'Express' before_each reset() end describe 'set("views")' it 'should default to <root>/views' set('root', 'spec') set('views').should.eql 'spec/views' end end
describe 'render()' endendNo newline at end of file