Changed; partial() "locals" are now optional
Before adoption: this commit came in with the history the project adopted, so no landing records it.
this means that: partial("user", { name: "tj" }) with the intent of receiving "user" instead of "name" in this case is invalid, "name" here is a local, however passing a non-plain object such as a User object is fine: partial("user", new User("tj"));
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 3f31ebc · Browse files · 6 files·+203 added−111 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+203 added−111 removed
- tests:+156 added−88 removed
- other:+47 added−23 removed
Provenance
- Commit
909914f- Tree
cd898b2- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
3f31ebc
6 files changed+203 added−111 removed
modified:examples/blog/views/index.jade+2 added-2 removed
8 unmodified lines9 p 10 | It looks like you have no posts!11 p12 | Click13- a(href=base + '/post/add') here13+ a(href=base + '/post/add') here 14 | to create a post. Login15 | as16- em "admin"16+ em "admin" 17 | and18 em "express"19 | .modified:examples/jade/app.js+13 added-4 removed
27 unmodified lines2829303132333435363738343536394041424344456 unmodified lines52535455565758515960616263644 unmodified lines27 unmodified lines// which prevents the need for extensions// (although you can still mix and match)app.set('view engine', 'jade');
function User(name, email) { this.name = name; this.email = email;}
// Dummy usersvar users = [ { name: 'tj', email: 'tj@vision-media.ca' } , { name: 'ciaran', email: 'ciaranj@gmail.com' } , { name: 'aaron', email: 'aaron.heckmann+github@gmail.com' } new User('tj', 'tj@vision-media.ca') , new User('ciaran', 'ciaranj@gmail.com') , new User('aaron', 'aaron.heckmann+github@gmail.com')];
app.get('/', function(req, res){ res.render('users', { users: users });6 unmodified lines res.partial('users/user', users);});
app.get('/users/list', function(req, res){ // use "object" to utilize the name deduced from // the view filename. The examples below are equivalent
res.partial('users/list', { object: users }); //res.partial('users/list', { object: users }); res.partial('users/list', { list: users });});
app.get('/user/:id', function(req, res){ res.partial('users/user', users[req.params.id]);4 unmodified linesmodified:lib/view.js+32 added-17 removed
45 unmodified lines46 *47 * @api private48 */49 50-function renderPartial(res, view, options, locals, parent){50+function renderPartial(res, view, options, parentLocals, parent){51+ var collection, object, locals;52+53 // Inherit parent view extension when not present54 if (parent && !~view.indexOf('.')) {55 view += parent.extension;56 }57 56- // Allow collection to be passed as second param58 if (options) {58- if ('length' in options) {59- options = { collection: options };60- } else if (!options.collection && !options.locals && !options.object) {61- options = { object: options };59+ // collection60+ if (options.collection) {61+ collection = options.collection;62+ delete options.collection;63+ } else if ('length' in options) {64+ collection = options;65+ options = {};66 }67+68+ // locals69+ if (options.locals) {70+ locals = options.locals;71+ delete options.locals;72+ }73+74+ // object75+ if ('Object' != options.constructor.name) {76+ object = options;77+ options = {};78+ } else if (undefined != options.object) {79+ object = options.object;80+ delete options.object;81+ }82 } else {83 options = {};84 }85 86 // Inherit locals from parent68- union(options, locals);87+ union(options, parentLocals);88 89 // Merge locals71- if (options.locals) {72- merge(options, options.locals);73- }90+ if (locals) merge(options, locals);91 92 // Partials dont need layouts93 options.renderPartial = true;94 options.layout = false;2 unmodified lines97 var name = options.as || partial.resolveObjectName(view);98 99 // Render partial100 function render(){84- if (options.object) {101+ if (object) {102 if ('string' == typeof name) {86- options[name] = options.object;103+ options[name] = object;104 } else if (name === global) {88- merge(options, options.object);105+ merge(options, object);106 } else {90- options.scope = options.object;107+ options.scope = object;108 }109 }110 return res.render(view, options, null, parent);111 }112 113 // Collection support97- var collection = options.collection;114 if (collection) {115 var len = collection.length116 , buf = '';101- delete options.collection;117 options.collectionLength = len;118 for (var i = 0; i < len; ++i) {119 var val = collection[i];120 options.firstInCollection = i === 0;121 options.indexInCollection = i;122 options.lastInCollection = i === len - 1;108- options.object = val;123+ object = val;124 buf += render();125 }126 return buf;127 } else {211 unmodified linesadded:test/fixtures/item-title.jade+1 added
1+li #{title} #{item}added:test/fixtures/stats.jade+2 added
1+p Hits #{hits}2+p Misses #{misses}modified:test/view.test.js+153 added-88 removed
103 unmodified lines104 105 'test #render()': function(){106 var app = create();107 app.set('view engine', 'jade');108-108+ 109 app.get('/', function(req, res){110 res.render('index.jade', { layout: false });111 });112-112+ 113 app.get('/jade', function(req, res){114 res.render('index', { layout: false });115 });116-116+ 117 app.get('/haml', function(req, res){118 res.render('hello.haml', { layout: false });119 });120-120+ 121 app.get('/callback/layout/no-options', function(req, res){122 res.render('hello.jade', function(err, str){123 assert.ok(!err);124 res.send(str.replace(':(', ':)'));125 });126 });127-127+ 128 app.get('/callback/layout', function(req, res){129 res.render('hello.jade', {}, function(err, str){130 assert.ok(!err);131 res.send(str.replace(':(', ':)'));132 });133 });134-134+ 135 app.get('/callback', function(req, res){136 res.render('hello.haml', { layout: false }, function(err, str){137 assert.ok(!err);138 res.send(str.replace('Hello World', ':)'));139 });140 });141-141+ 142 app.get('/invalid', function(req, res){143 res.render('invalid.jade', { layout: false });144 });145-145+ 146 app.get('/invalid-async', function(req, res){147 process.nextTick(function(){148 res.render('invalid.jade', { layout: false });149 });150 });151-151+ 152 app.get('/error', function(req, res){153 res.render('invalid.jade', { layout: false }, function(err){154 res.send(err.arguments[0]);155 });156 });157-157+ 158 app.get('/absolute', function(req, res){159 res.render(__dirname + '/fixtures/index.jade', { layout: false });160 });161-161+ 162 app.get('/ferret', function(req, res){163 res.render('ferret', { layout: false, ferret: { name: 'Tobi' }});164 });165-165+ 166 app.get('/status', function(req, res){167 res.render('hello.jade', { status: 500 });168 });169-169+ 170 assert.response(app,171 { url: '/status' },172 { status: 500 });173 assert.response(app,39 unmodified lines213 214 'test #render() layout': function(){215 var app = create();216 app.set('view engine', 'jade');217-217+ 218 app.get('/', function(req, res){219 res.render('index.jade');220 });221 app.get('/jade', function(req, res){222 res.render('index');223 });224-224+ 225 assert.response(app,226 { url: '/' },227 { body: '<html><body><p>Welcome</p></body></html>' });228 },229 230 'test #render() specific layout': function(beforeExit){231 var app = create()232 , called;233-233+ 234 app.get('/', function(req, res){235 res.render('index.jade', { layout: 'cool-layout.jade' }, function(err, html){236 called = true;237 res.send(html);10 unmodified lines248 });249 app.get('/nope', function(req, res){250 res.render('index.jade', { layout: 'nope.jade' });251 });252-252+ 253 assert.response(app,254 { url: '/' },255 { body: '<cool><p>Welcome</p></cool>' });256 assert.response(app,10 unmodified lines267 function(res){268 assert.ok(~res.body.indexOf('Error: failed to locate view'));269 assert.ok(~res.body.indexOf('nope'));270 });271-271+ 272 beforeExit(function(){273 assert.ok(called, 'Layout callback never called'); 274 });275 },26 unmodified lines302 303 'test #render() view helpers': function(beforeExit){304 var app = create()305 , calls = 0;306-306+ 307 app.locals({308 lastName: 'holowaychuk'309 });310-310+ 311 app.helpers({ 312 greetings: function(sess, lastName){313 return 'Hello ' + sess.name + ' ' + lastName; 314 }315 });316-316+ 317 var ret = app.dynamicHelpers({318 session: function(req, res){319 ++calls;320 req.should.be.a('object');1 unmodified line322 this.should.equal(app);323 return req.session;324 }325 });326-326+ 327 assert.equal(app, ret, 'Server#helpers() is not chainable');328 329 app.get('/', function(req, res){330 req.session = { name: 'tj' };331 res.render('dynamic-helpers.jade', { layout: false });332 });333-333+ 334 app.get('/ejs', function(req, res){335 req.session = { name: 'tj' };336 res.render('dynamic-helpers.ejs', { layout: false });337 });338-338+ 339 app.get('/precedence', function(req, res){340 req.session = { name: 'tj' };341 res.render('dynamic-helpers.jade', {342 lastName: 'foobar'8 unmodified lines351 { body: '<p>Hello tj holowaychuk</p>' });352 assert.response(app,353 { url: '/precedence' },354 { body: '<html><body><p>Hello tj foobar</p></body></html>' });355-355+ 356 beforeExit(function(){357 assert.equal(3, calls);358 });359 },360 361 'test #partial()': function(){362 var app = create();363-363+ 364 app.set('view engine', 'jade');365-365+ 366 // Auto-assigned local w/ collection option367 app.get('/', function(req, res){368 res.render('items.jade', { items: ['one', 'two'] });369 });370 371 assert.response(app,372 { url: '/' },373 { body: '<html><body><ul><li>one</li><li>two</li></ul></body></html>' });374-374+ 375 // Auto-assigned local w/ collection array 376 var movies = [377 { title: 'Nightmare Before Christmas', director: 'Tim Burton' },378 { title: 'Avatar', director: 'James Cameron' }379 ];380 app.get('/movies', function(req, res){381 res.render('movies.jade', { movies: movies });382 });383- 383+ 384 var html = [385 '<html>',386 '<body>',387 '<ul>',8 unmodified lines396 '</ul>',397 '</body>',398 '</html>'399 ].join('');400-400+ 401 assert.response(app,402 { url: '/movies' },403 { body: html });404-404+ 405 // as: str collection option406 app.get('/user', function(req, res){407 res.partial('user', {408 as: 'person',3 unmodified lines412 413 assert.response(app,414 { url: '/user' },415 { body: '<p>tj</p>' });416-416+ 417 // as: with object collection418 app.get('/user/object', function(req, res){419 res.partial('user.jade', {420 as: 'person',3 unmodified lines424 425 assert.response(app,426 { url: '/user' },427 { body: '<p>tj</p>' });428- 428+429 // as: this collection option430 app.get('/person', function(req, res){431 res.partial('person.jade', {432 as: this,4 unmodified lines437 438 assert.response(app,439 { url: '/person' },440 { body: '<p>name: tj</p>' });441-441+ 442 // as: global collection option443 app.get('/videos', function(req, res){444 res.partial('video.jade', {445 as: global,446 collection: movies447 });448 });449-449+ 450 assert.response(app,451 { url: '/videos' },452 { body: '<p>Tim Burton</p><p>James Cameron</p>' });453 18 unmodified lines472 473 assert.response(app,474 { url: '/movie' },475 { body: '<li><div class="title">Nightmare Before Christmas</div><div class="director">Tim Burton</div></li>' });476- 476+477 app.get('/video-global', function(req, res){478 res.partial('video.jade', {479 object: movies[0],480 as: global3 unmodified lines484 // Non-collection as: global485 assert.response(app,486 { url: '/video-global' },487 { body: '<p>Tim Burton</p>' });488-488+ 489 app.get('/person-this', function(req, res){490 res.partial('person.jade', {491 object: { name: 'tj' },492 locals: { label: 'User:' },4 unmodified lines497 // Non-collection as: this498 assert.response(app,499 { url: '/person-this' },500 { body: '<p>User: tj</p>' });501-501+ 502 // No options503 app.get('/nothing', function(req, res){504 res.partial('hello.ejs');505 });506 507 assert.response(app,508 { url: '/nothing' },509 { body: 'Hello' });510-510+ 511 // Path segments + "as"512 app.get('/role/as', function(req, res){513 res.partial('user/role.ejs', { as: 'role', collection: ['admin', 'member'] });514 });515-515+ 516 assert.response(app,517 { url: '/role/as' },518 { body: '<li>Role: admin</li><li>Role: member</li>' });519-519+ 520 // Deduce name from last segment521 app.get('/role', function(req, res){522 res.partial('user/role.ejs', ['admin', 'member']);523 });524-524+ 525 assert.response(app,526 { url: '/role' },527 { body: '<li>Role: admin</li><li>Role: member</li>' });528+ 529+ // Non-basic object support530+ function Movie(title, director){531+ this.title = title;532+ this.director = director;533+ }534+ 535+ app.get('/movie/object', function(req, res){536+ res.partial('movie', new Movie('The TJ', 'tj'));537+ });538+ 539+ assert.response(app,540+ { url: '/movie/object' },541+ { body: '<li><div class="title">The TJ</div><div class="director">tj</div></li>' });542+543+ // Locals544+ app.get('/stats', function(req, res){545+ res.partial('stats', {546+ hits: 12547+ , misses: 1548+ });549+ });550+551+ assert.response(app,552+ { url: '/stats' },553+ { body: '<p>Hits 12</p><p>Misses 1</p>' });554+555+ // Locals556+ app.get('/stats/locals', function(req, res){557+ res.partial('stats', {558+ locals: {559+ hits: 12560+ , misses: 1 561+ }562+ });563+ });564+565+ assert.response(app,566+ { url: '/stats/locals' },567+ { body: '<p>Hits 12</p><p>Misses 1</p>' });568+569+ // Collection + locals570+ app.get('/items', function(req, res){571+ res.partial('item-title', {572+ collection: ['foo', 'bar']573+ , title: 'test'574+ , as: 'item'575+ });576+ });577+ 578+ assert.response(app,579+ { url: '/items' },580+ { body: '<li>test foo</li><li>test bar</li>' });581 },582 583 'test #partial() with several calls': function(){584 var app = create();532-585+ 586 app.get('/', function(req, res, next){587 res.render('list.jade', { layout: false });588 });589 3 unmodified lines593 },594 595 'test #partial() with several calls using locals': function(){596 var app = create();544-597+ 598 app.get('/', function(req, res, next){599 res.render('list2.jade', { layout: false });600 });601 3 unmodified lines605 },606 607 'test #partial() locals': function(){608 var app = create();556-609+ 610 app.set('view engine', 'jade');558-611+ 612 app.get('/', function(req, res, next){613 res.partial('pet-count', {614 locals: {615 pets: {1 unmodified line617 }618 }619 });620 });568-621+ 622 assert.response(app,623 { url: '/' },624 { body: 'We have 5 cool pets\n' });625 },626 627 'test #partial() locals precedence': function(){628 var app = create();576-629+ 630 app.get('/', function(req, res, next){631 res.render('greetings.jade', {632 name: 'TJ'633 , locals: { otherName: 'Overridden' }634 });635 });583-636+ 637 assert.response(app,638 { url: '/' },639 { body: '<html><body><h1>TJ</h1><p>Welcome Overridden</p></body></html>' });640 },588-641+ 642 'test #partial() index': function(){643 var app = create();591-644+ 645 app.set('view engine', 'jade');646 647+ function Ferret(name){ this.name = name; };648+649 app.get('/ferret', function(req, res){595- res.partial('ferret', { name: 'Tobi' });650+ res.partial('ferret', new Ferret('Tobi'));651 });652+ 653+ app.get('/ferret/basic', function(req, res){654+ res.partial('ferret', { ferret: { name: 'Tobi' }});655+ });656 657 assert.response(app,658+ { url: '/ferret/basic' },659+ { body: '<li class="ferret">Tobi</li>' });660+661+ assert.response(app,662 { url: '/ferret' },663 { body: '<li class="ferret">Tobi</li>' });664 },602-665+ 666 'test #partial() relative index': function(){667 var app = create();668 669 app.set('view engine', 'jade');670 671+ function Ferret(name) { this.name = name; }672 app.get('/ferret', function(req, res){609- var tobi = { name: 'Tobi' }610- , loki = { name: 'Loki' };673+ var tobi = new Ferret('Tobi')674+ , loki = new Ferret('Loki');675 res.partial('ferret/list', { object: [tobi, loki] });676 });613-677+ 678 assert.response(app,679 { url: '/ferret' },680 { body: '<ul id="ferrets"><li class="ferret">Tobi</li><li class="ferret">Loki</li></ul>' });681 },618-682+ 683 'test #partial() object': function(){684 var app = create();621-685+ 686 app.get('/', function(req, res, next){623- res.partial('movie.jade', {624- title: 'Foobar'625- , director: 'Tim Burton'626- });687+ function Movie(title, director) {688+ this.title = title;689+ this.director = director;690+ }691+ res.partial('movie.jade', new Movie('Foobar', 'Tim Burton'));692 });628-693+ 694 assert.response(app,695 { url: '/' },696 { body: '<li><div class="title">Foobar</div><div class="director">Tim Burton</div></li>' });697 },698 699 'test #partial() locals with collection': function(){700 var app = create();636-701+ 702 app.get('/', function(req, res, next){703 res.render('pet-land.jade', {704 pets: ['Ewald']705 });706 });642-707+ 708 assert.response(app,709 { url: '/' },710 { body: '<html><body><div><li>Ewald is the coolest of Animal land</li></div></body></html>' });711 },712 713 'test #partial() inheriting initial locals': function(){714 var app = create();650-715+ 716 app.get('/pets', function(req, res, next){717 res.render('pets.jade', {718 site: 'My Cool Pets'719 , pets: ['Tobi', 'Jane', 'Bandit']720 });721 });657-722+ 723 var html = [724 '<html>',725 '<body>',726 '<h1>My Cool Pets</h1>',5 unmodified lines732 '</ul>',733 '</body>',734 '</html>'735 ].join('');671-736+ 737 assert.response(app,738 { url: '/pets' },739 { body: html });740 },741 742 'test #partial() with array-like collection': function(){743 var app = create();679-744+ 745 var movies = {746 0: { title: 'Nightmare Before Christmas', director: 'Tim Burton' },747 1: { title: 'Avatar', director: 'James Cameron' },748 length: 217 unmodified lines766 '</ul>',767 '</body>',768 '</html>'769 ].join('');705-770+ 771 assert.response(app,772 { url: '/movies' },773 { body: html });774 },775 776 'test "partials" setting': function(){777 var app = create();778 app.set('partials', __dirname + '/fixtures/sub-templates');714-779+ 780 app.get('/', function(req, res){781 res.render('items.jade', {782 layout: false,783 items: ['foo', 'bar']88 unmodified lines872 layout: false873 , open: '<?'874 , title: 'Original'875 });811-876+ 877 function setTitle(req, res, next) {878 res.local('title', 'Wahoo');879 next();880 }816-881+ 882 app.get('/video', setTitle, function(req, res, next){883 res.local('close', '?>');884 res.render('video.ejs', { layout: false, title: 'keyboard cat' });885 });1 unmodified line887 app.get('/video/2', setTitle, function(req, res, next){888 res.local('close', '?>');889 res.render('video.ejs', { layout: false });890 });826-891+ 892 app.get('/video/3', function(req, res, next){893 res.local('close', '?>');894 res.render('video.ejs', { layout: false });895 });831-896+ 897 assert.response(app,898 { url: '/video' },899 { body: '<h1>keyboard cat</h1>' });835-900+ 901 assert.response(app,902 { url: '/video/2' },903 { body: '<h1>Wahoo</h1>' });839-904+ 905 assert.response(app,906 { url: '/video/3' },907 { body: '<h1>Original</h1>' });908 },16 unmodified lines925 pets.push('Jane', 'Bandit');926 }927 next();928 });864-929+ 930 app.get('/pets', function(req, res, next){931 res.render('pets.jade');932 });868-933+ 934 var html = [935 '<html>',936 '<body>',937 '<h1>My Cool Pets</h1>',5 unmodified lines943 '</ul>',944 '</body>',945 '</html>'946 ].join('');882-947+ 948 assert.response(app,949 { url: '/pets' },950 { body: html });951 },952 953 'test .charset with res.render()': function(){954 var app = create();890-955+ 956 app.get('/', function(req, res){957 res.charset = 'ISO-8859-1';958 res.render('hello.jade');959 });895-960+ 961 assert.response(app,962 { url: '/' },963 { headers: { 'Content-Type': 'text/html; charset=ISO-8859-1' }});964 },965 966 'test charset res.render() option': function(){967 var app = create();903-968+ 969 app.get('/', function(req, res){970 res.render('hello.jade', { charset: 'ISO-8859-1' });971 });907-972+ 973 assert.response(app,974 { url: '/' },975 { headers: { 'Content-Type': 'text/html; charset=ISO-8859-1' }});976 },977 978 'test charset option': function(){979 var app = create();980 app.set('view options', { charset: 'ISO-8859-1' });916-981+ 982 app.get('/', function(req, res){983 res.render('hello.jade');984 });920-985+ 986 assert.response(app,987 { url: '/' },988 { headers: { 'Content-Type': 'text/html; charset=ISO-8859-1' }});989 },990 991 'test charset override': function(){992 var app = create();993 app.set('view options', { charset: 'ISO-8859-1' });929-994+ 995 app.get('/', function(req, res){996 res.render('hello.jade', { charset: 'utf8' });997 });933-998+ 999 assert.response(app,1000 { url: '/' },1001 { headers: { 'Content-Type': 'text/html; charset=utf8' }});1002 }1 unmodified line