Skip to content
Myr

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

6 files+203 added−111 removed

modified:examples/blog/views/index.jade+2 added-2 removed
8 unmodified lines
9 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
modified:lib/view.js+32 added-17 removed
45 unmodified lines
46 *47 * @api private48 */4950function renderPartial(res, view, options, locals, parent){50function renderPartial(res, view, options, parentLocals, parent){51 var collection, object, locals;5253 // Inherit parent view extension when not present54 if (parent && !~view.indexOf('.')) {55 view += parent.extension;56 }5756 // 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 }6768 // locals69 if (options.locals) {70 locals = options.locals;71 delete options.locals;72 }7374 // 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 }8586 // 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);9192 // Partials dont need layouts93 options.renderPartial = true;94 options.layout = false;
2 unmodified lines
97 var name = options.as || partial.resolveObjectName(view);9899 // 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 }112113 // 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 lines
added:test/fixtures/item-title.jade+1 added
1li #{title} #{item}
added:test/fixtures/stats.jade+2 added
1p Hits #{hits}2p Misses #{misses}
modified:test/view.test.js+153 added-88 removed
103 unmodified lines
104 105 'test #render()': function(){106 var app = create();107 app.set('view engine', 'jade');108108 109 app.get('/', function(req, res){110 res.render('index.jade', { layout: false });111 });112112 113 app.get('/jade', function(req, res){114 res.render('index', { layout: false });115 });116116 117 app.get('/haml', function(req, res){118 res.render('hello.haml', { layout: false });119 });120120 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 });127127 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 });134134 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 });141141 142 app.get('/invalid', function(req, res){143 res.render('invalid.jade', { layout: false });144 });145145 146 app.get('/invalid-async', function(req, res){147 process.nextTick(function(){148 res.render('invalid.jade', { layout: false });149 });150 });151151 152 app.get('/error', function(req, res){153 res.render('invalid.jade', { layout: false }, function(err){154 res.send(err.arguments[0]);155 });156 });157157 158 app.get('/absolute', function(req, res){159 res.render(__dirname + '/fixtures/index.jade', { layout: false });160 });161161 162 app.get('/ferret', function(req, res){163 res.render('ferret', { layout: false, ferret: { name: 'Tobi' }});164 });165165 166 app.get('/status', function(req, res){167 res.render('hello.jade', { status: 500 });168 });169169 170 assert.response(app,171 { url: '/status' },172 { status: 500 });173 assert.response(app,
39 unmodified lines
213 214 'test #render() layout': function(){215 var app = create();216 app.set('view engine', 'jade');217217 218 app.get('/', function(req, res){219 res.render('index.jade');220 });221 app.get('/jade', function(req, res){222 res.render('index');223 });224224 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;233233 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 lines
248 });249 app.get('/nope', function(req, res){250 res.render('index.jade', { layout: 'nope.jade' });251 });252252 253 assert.response(app,254 { url: '/' },255 { body: '<cool><p>Welcome</p></cool>' });256 assert.response(app,
10 unmodified lines
267 function(res){268 assert.ok(~res.body.indexOf('Error: failed to locate view'));269 assert.ok(~res.body.indexOf('nope'));270 });271271 272 beforeExit(function(){273 assert.ok(called, 'Layout callback never called'); 274 });275 },
26 unmodified lines
302 303 'test #render() view helpers': function(beforeExit){304 var app = create()305 , calls = 0;306306 307 app.locals({308 lastName: 'holowaychuk'309 });310310 311 app.helpers({ 312 greetings: function(sess, lastName){313 return 'Hello ' + sess.name + ' ' + lastName; 314 }315 });316316 317 var ret = app.dynamicHelpers({318 session: function(req, res){319 ++calls;320 req.should.be.a('object');
1 unmodified line
322 this.should.equal(app);323 return req.session;324 }325 });326326 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 });333333 334 app.get('/ejs', function(req, res){335 req.session = { name: 'tj' };336 res.render('dynamic-helpers.ejs', { layout: false });337 });338338 339 app.get('/precedence', function(req, res){340 req.session = { name: 'tj' };341 res.render('dynamic-helpers.jade', {342 lastName: 'foobar'
8 unmodified lines
351 { body: '<p>Hello tj holowaychuk</p>' });352 assert.response(app,353 { url: '/precedence' },354 { body: '<html><body><p>Hello tj foobar</p></body></html>' });355355 356 beforeExit(function(){357 assert.equal(3, calls);358 });359 },360 361 'test #partial()': function(){362 var app = create();363363 364 app.set('view engine', 'jade');365365 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>' });374374 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 lines
396 '</ul>',397 '</body>',398 '</html>'399 ].join('');400400 401 assert.response(app,402 { url: '/movies' },403 { body: html });404404 405 // as: str collection option406 app.get('/user', function(req, res){407 res.partial('user', {408 as: 'person',
3 unmodified lines
412 413 assert.response(app,414 { url: '/user' },415 { body: '<p>tj</p>' });416416 417 // as: with object collection418 app.get('/user/object', function(req, res){419 res.partial('user.jade', {420 as: 'person',
3 unmodified lines
424 425 assert.response(app,426 { url: '/user' },427 { body: '<p>tj</p>' });428 428429 // as: this collection option430 app.get('/person', function(req, res){431 res.partial('person.jade', {432 as: this,
4 unmodified lines
437 438 assert.response(app,439 { url: '/person' },440 { body: '<p>name: tj</p>' });441441 442 // as: global collection option443 app.get('/videos', function(req, res){444 res.partial('video.jade', {445 as: global,446 collection: movies447 });448 });449449 450 assert.response(app,451 { url: '/videos' },452 { body: '<p>Tim Burton</p><p>James Cameron</p>' });453
18 unmodified lines
472 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 476477 app.get('/video-global', function(req, res){478 res.partial('video.jade', {479 object: movies[0],480 as: global
3 unmodified lines
484 // Non-collection as: global485 assert.response(app,486 { url: '/video-global' },487 { body: '<p>Tim Burton</p>' });488488 489 app.get('/person-this', function(req, res){490 res.partial('person.jade', {491 object: { name: 'tj' },492 locals: { label: 'User:' },
4 unmodified lines
497 // Non-collection as: this498 assert.response(app,499 { url: '/person-this' },500 { body: '<p>User: tj</p>' });501501 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' });510510 511 // Path segments + "as"512 app.get('/role/as', function(req, res){513 res.partial('user/role.ejs', { as: 'role', collection: ['admin', 'member'] });514 });515515 516 assert.response(app,517 { url: '/role/as' },518 { body: '<li>Role: admin</li><li>Role: member</li>' });519519 520 // Deduce name from last segment521 app.get('/role', function(req, res){522 res.partial('user/role.ejs', ['admin', 'member']);523 });524524 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>' });542543 // Locals544 app.get('/stats', function(req, res){545 res.partial('stats', {546 hits: 12547 , misses: 1548 });549 });550551 assert.response(app,552 { url: '/stats' },553 { body: '<p>Hits 12</p><p>Misses 1</p>' });554555 // Locals556 app.get('/stats/locals', function(req, res){557 res.partial('stats', {558 locals: {559 hits: 12560 , misses: 1 561 }562 });563 });564565 assert.response(app,566 { url: '/stats/locals' },567 { body: '<p>Hits 12</p><p>Misses 1</p>' });568569 // 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();532585 586 app.get('/', function(req, res, next){587 res.render('list.jade', { layout: false });588 });589
3 unmodified lines
593 },594 595 'test #partial() with several calls using locals': function(){596 var app = create();544597 598 app.get('/', function(req, res, next){599 res.render('list2.jade', { layout: false });600 });601
3 unmodified lines
605 },606 607 'test #partial() locals': function(){608 var app = create();556609 610 app.set('view engine', 'jade');558611 612 app.get('/', function(req, res, next){613 res.partial('pet-count', {614 locals: {615 pets: {
1 unmodified line
617 }618 }619 });620 });568621 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();576629 630 app.get('/', function(req, res, next){631 res.render('greetings.jade', {632 name: 'TJ'633 , locals: { otherName: 'Overridden' }634 });635 });583636 637 assert.response(app,638 { url: '/' },639 { body: '<html><body><h1>TJ</h1><p>Welcome Overridden</p></body></html>' });640 },588641 642 'test #partial() index': function(){643 var app = create();591644 645 app.set('view engine', 'jade');646647 function Ferret(name){ this.name = name; };648649 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 });656657 assert.response(app,658 { url: '/ferret/basic' },659 { body: '<li class="ferret">Tobi</li>' });660661 assert.response(app,662 { url: '/ferret' },663 { body: '<li class="ferret">Tobi</li>' });664 },602665 666 'test #partial() relative index': function(){667 var app = create();668669 app.set('view engine', 'jade');670671 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 });613677 678 assert.response(app,679 { url: '/ferret' },680 { body: '<ul id="ferrets"><li class="ferret">Tobi</li><li class="ferret">Loki</li></ul>' });681 },618682 683 'test #partial() object': function(){684 var app = create();621685 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 });628693 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();636701 702 app.get('/', function(req, res, next){703 res.render('pet-land.jade', {704 pets: ['Ewald']705 });706 });642707 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();650715 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 });657722 723 var html = [724 '<html>',725 '<body>',726 '<h1>My Cool Pets</h1>',
5 unmodified lines
732 '</ul>',733 '</body>',734 '</html>'735 ].join('');671736 737 assert.response(app,738 { url: '/pets' },739 { body: html });740 },741 742 'test #partial() with array-like collection': function(){743 var app = create();679744 745 var movies = {746 0: { title: 'Nightmare Before Christmas', director: 'Tim Burton' },747 1: { title: 'Avatar', director: 'James Cameron' },748 length: 2
17 unmodified lines
766 '</ul>',767 '</body>',768 '</html>'769 ].join('');705770 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');714779 780 app.get('/', function(req, res){781 res.render('items.jade', {782 layout: false,783 items: ['foo', 'bar']
88 unmodified lines
872 layout: false873 , open: '<?'874 , title: 'Original'875 });811876 877 function setTitle(req, res, next) {878 res.local('title', 'Wahoo');879 next();880 }816881 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 line
887 app.get('/video/2', setTitle, function(req, res, next){888 res.local('close', '?>');889 res.render('video.ejs', { layout: false });890 });826891 892 app.get('/video/3', function(req, res, next){893 res.local('close', '?>');894 res.render('video.ejs', { layout: false });895 });831896 897 assert.response(app,898 { url: '/video' },899 { body: '<h1>keyboard cat</h1>' });835900 901 assert.response(app,902 { url: '/video/2' },903 { body: '<h1>Wahoo</h1>' });839904 905 assert.response(app,906 { url: '/video/3' },907 { body: '<h1>Original</h1>' });908 },
16 unmodified lines
925 pets.push('Jane', 'Bandit');926 }927 next();928 });864929 930 app.get('/pets', function(req, res, next){931 res.render('pets.jade');932 });868933 934 var html = [935 '<html>',936 '<body>',937 '<h1>My Cool Pets</h1>',
5 unmodified lines
943 '</ul>',944 '</body>',945 '</html>'946 ].join('');882947 948 assert.response(app,949 { url: '/pets' },950 { body: html });951 },952 953 'test .charset with res.render()': function(){954 var app = create();890955 956 app.get('/', function(req, res){957 res.charset = 'ISO-8859-1';958 res.render('hello.jade');959 });895960 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();903968 969 app.get('/', function(req, res){970 res.render('hello.jade', { charset: 'ISO-8859-1' });971 });907972 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' });916981 982 app.get('/', function(req, res){983 res.render('hello.jade');984 });920985 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' });929994 995 app.get('/', function(req, res){996 res.render('hello.jade', { charset: 'utf8' });997 });933998 999 assert.response(app,1000 { url: '/' },1001 { headers: { 'Content-Type': 'text/html; charset=utf8' }});1002 }
1 unmodified line