add cache view partials
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Aaron Heckmann · committed by Aaron Heckmann · parent 33277c3 · Browse files · 3 files·+29 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+29 added−4 removed
- tests:+8 added−0 removed
- other:+21 added−4 removed
Provenance
- Commit
50e533c- Tree
5a891ba- Author
Aaron Heckmann <aaron.heckmann@gmail.com>- Committer
Aaron Heckmann <aaron.heckmann@gmail.com>- Parent
33277c3
3 files changed+29 added−4 removed
modified:lib/express/plugins/view.js+20 added-4 removed
12 unmodified lines131415161718192021222324258 unmodified lines34353637383940414243444546474849505121 unmodified lines737475766277787965808182838423 unmodified lines1081091101119711211311411511612 unmodified lines12913013113211813313413513613713818 unmodified lines12 unmodified lines */
var engines = {}
/** * Cache partial store */var partialcache = {}
// --- View
exports.View = Plugin.extend({ extend: {8 unmodified lines if (!set('views')) set('views', function(){ return set('root') + '/views' }) if (!set('partials')) set('partials', function(){ return set('root') + '/views/partials' }) if (set('cache view partials')) fs.readdirSync(set('partials')).each(function(file){ partialcache[file] = fs.readFileSync(file) }) // Request Request.include({ 21 unmodified lines var name = options.as || view.split('.').first options.locals = options.locals || {} return options.collection.map(function(val){ options.locals[name] = val return this.render('partials/' + view, options) return this.render(view, options) }, this).join('') } else return this.render('partials/' + view, options) return this.render(view, options) },
/** * Render _view_ with _options_.23 unmodified lines render: function(view, options) { var self = this, options = options || {}, partial = options.partial, path = set('views') + '/' + view, path = set(partial ? 'partials' : 'views') + '/' + view, type = path.split('.').slice(-2)[0], ext = extname(path), layout = options.layout === undefined ? 'layout' : options.layout options.context = options.context || this12 unmodified lines else self.halt(200, content) } function renderFromDisc() { if (partial) return render(fs.readFileSync(path)) if (partial) return render( partialcache[path] || fs.readFileSync(path) ) fs.readFile(path, function(err, content){ if (err) throw err set('cache view contents') ? self.cache.set('view:' + path, content, function(cache){18 unmodified linesmodified:lib/express/core.js+1 added
341 unmodified lines342 })343 344 configure('production', function(){345 enable('cache view contents')346+ enable('cache view partials')347 enable('cache static files')348 })modified:spec/spec.plugins.view.js+8 added
10 unmodified lines11 set('views').should.eql 'spec/views'12 end13 end14 15+ describe 'set("partials")'16+ it 'should default to <root>/views/partials'17+ set('root', 'spec')18+ set('partials').should.eql 'spec/views/partials'19+ end20+ end21+ 22 describe '#partial()'23 before_each24 set('views', 'spec/fixtures')25+ set('partials', 'spec/fixtures/partials')26 end27 28 describe 'given a valid view name'29 it 'should render a partial'169 unmodified lines