Started view partial support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent d61c248 · Browse files · 4 files·+46 added−5 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+46 added−5 removed
- tests:+21 added−0 removed
- other:+25 added−5 removed
Provenance
- Commit
346f019- Tree
d4e7f57- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
d61c248
4 files changed+46 added−5 removed
modified:lib/express/plugins/view.js+25 added-5 removed
28 unmodified lines293031323333343536373839404142434445464748495051525354555621 unmodified lines78798081828384858687718889909192939495969798991001011021031041051061071083 unmodified lines11211311411596971161171181191001201211221231242 unmodified lines28 unmodified lines // Settings if (!set('views')) set('views', function(){ return set('root') + '/views' }) // Request Request.include({ /** * Render _view_ partial with _options_. * * @param {string} view * @param {hash} options * @return {string} * @api public */ partial: function(view, options) { var options = options || {} options.partial = true options.layout = false return this.render('partials/' + view, options) },
/** * Render _view_ with _options_. *21 unmodified lines
render: function(view, options) { var self = this, options = options || {}, partial = options.partial, path = set('views') + '/' + view, type = path.split('.').slice(-2)[0], ext = extname(path), layout = options.layout === undefined ? 'layout' : options.layout options.context = options.context || this self.contentType(ext) if (!partial) self.contentType(ext) function render(content) { content = (engines[type] = engines[type] || require(type)).render(content, options) if (layout) self.render(layout + '.' + type + ext, utils.mixin(true, options, { layout: false, locals: { body: content } })) else if (partial) return content else self.halt(200, content) } function renderFromDisc() { if (partial) return render(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){3 unmodified lines }) } if (set('cache view contents')) self.cache.get('view:' + path, function(cache){ if (cache) render(cache) else renderFromDisc() if (cache) return render(cache) else return renderFromDisc() }) else renderFromDisc() return renderFromDisc() } }) } }2 unmodified linesadded:spec/fixtures/partials/item.haml.html+1 added
1%li= itemNo newline at end of fileadded:spec/fixtures/list.haml.html+3 added
123%ul :each item in items = this.partial('item.haml.html', { locals: { item: item }})No newline at end of filemodified:spec/spec.plugins.view.js+17 added
10 unmodified lines11 set('views').should.eql 'spec/views'12 end13 end14 15+ describe '#partial()'16+ before_each17+ set('views', 'spec/fixtures')18+ end19+ 20+ describe 'given a valid view name'21+ it 'should render a partial'22+ get('/', function(){23+ this.render('list.haml.html', { locals: { items: ['foo', 'bar'] }})24+ })25+ get('/').body.should.include '<ul>'26+ get('/').body.should.include '<li>foo'27+ get('/').body.should.include '<li>bar'28+ end29+ end30+ end31+ 32 describe '#render()'33 before_each34 set('views', 'spec/fixtures')35 end134 unmodified lines