Merge branch 'partials'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parents d61c248, 5d10ee4 · Browse files · 7 files·+85 added−7 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+85 added−7 removed
- tests:+43 added−0 removed
- other:+42 added−7 removed
Provenance
- Commit
28f2ad0- Tree
5fe1e80- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
d61c248- Parent 2
5d10ee4
7 files changed+85 added−7 removed
added:examples/chat/views/partials/message.haml.html+1 added
1%li= messageNo newline at end of filemodified:examples/chat/views/chat.haml.html+1 added-2 removed
12345456789%h1 Chat%img.bubble{ src: '/public/images/bubble.png' }%ul#messages :each msg in messages %li= msg = this.partial('message.haml.html', { collection: messages })%form{ method: 'post' } %input{ type: 'hidden', name: '_method', value: 'put' } %input{ type: 'text', name: 'name', value: name || 'guest' } %input{ type: 'text', name: 'message' } %input{ type: 'submit', value: 'Send' }modified:lib/express/plugins/view.js+40 added-5 removed
28 unmodified lines293031323333343536373839404142434445464748495051525354555657585960616263646566676869707121 unmodified lines93949596979899100101102711031041051061071081091101111121131141151161171181191201211221233 unmodified lines12712812913096971311321331341001351361371381392 unmodified lines28 unmodified lines // Settings if (!set('views')) set('views', function(){ return set('root') + '/views' }) // Request Request.include({ /** * Render _view_ partial with _options_. * View Request#render() for additional options. * * Options: * - as: String name for the id used to which "collection" assign it's current value. * - collection: Array of objects, the name is derived from * the view name itself. For example 'video.haml.html' * will have an object "video" available to it. * * @param {string} view * @param {hash} options * @return {string} * @api public */ partial: function(view, options) { var options = options || {} options.partial = true options.layout = false if (options.collection) { 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) }, this).join('') } else 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/partials/video.haml.html+1 added
1%li= vidNo 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+38 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+ 30+ it 'should render collections'31+ get('/', function(){32+ return this.partial('item.haml.html', {33+ collection: ['foo', 'bar']34+ })35+ })36+ get('/').body.should.include '<li>foo'37+ get('/').body.should.include '<li>bar'38+ end39+ 40+ it 'should render collections with a given object name'41+ get('/', function(){42+ return this.partial('video.haml.html', {43+ collection: ['im a movie', 'im another movie'],44+ as: 'vid'45+ })46+ })47+ get('/').body.should.include '<li>im a movie'48+ get('/').body.should.include '<li>im another movie'49+ end50+ end51+ end52+ 53 describe '#render()'54 before_each55 set('views', 'spec/fixtures')56 end134 unmodified lines