Skip to content
Myr

Added res.partial(), using the same interface as partial() within a view. Closes #539

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 5093127 · Browse files · 3 files+83 added−47 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+83 added−47 removed

  • tests:+32 added−24 removed
  • other:+51 added−23 removed

3 files+83 added−47 removed

modified:examples/partials/app.js+5 added
modified:lib/view.js+46 added-23 removed
16 unmodified lines
17 , View = require('./view/view')18 , Partial = require('./view/partial')19 , union = require('./utils').union20 , merge = utils.merge21 , http = require('http');21 , http = require('http')22 , res = http.ServerResponse.prototype;2324/**25 * Memory cache.26 *
15 unmodified lines
4243exports.register = View.register;4445/**45 * Render `view` partial with the given `options`.46 * Partial render helper.47 *47 * Options:48 * - `object` Single object with name derived from the view (unless `as` is present) 49 *50 * - `as` Variable name for each `collection` value, defaults to the view name.51 * * as: 'something' will add the `something` local variable52 * * as: this will use the collection value as the template context53 * * as: global will merge the collection value's properties with `locals`54 *55 * - `collection` Array of objects, the name is derived from the view name itself. 56 * For example _video.html_ will have a object _video_ available to it.57 *58 * @param {String} view59 * @param {Object|Array} options, collection, or object60 * @return {String}61 * @api public48 * @api private49 */5064http.ServerResponse.prototype.partial = function(view, options, locals, parent){65 var self = this;6651function renderPartial(res, view, options, locals, parent){52 // Inherit parent view extension when not present53 if (parent && !~view.indexOf('.')) {54 view += parent.extension;55 }
34 unmodified lines
90 } else {91 options.scope = options.object;92 }93 }109 return self.render(view, options, null, parent);94 return res.render(view, options, null, parent);95 }9697 // Collection support98 var collection = options.collection;
16 unmodified lines
115 }116};117118/**119 * Render `view` partial with the given `options`.120 *121 * Options:122 *123 * - `object` Single object with name derived from the view (unless `as` is present) 124 *125 * - `as` Variable name for each `collection` value, defaults to the view name.126 * * as: 'something' will add the `something` local variable127 * * as: this will use the collection value as the template context128 * * as: global will merge the collection value's properties with `locals`129 *130 * - `collection` Array of objects, the name is derived from the view name itself. 131 * For example _video.html_ will have a object _video_ available to it.132 *133 * @param {String} view134 * @param {Object|Array} options, collection, or object135 * @return {String}136 * @api public137 */138139res.partial = function(view, options){140 var app = this.app141 , options = options || {}142 , parent = {};143144 // root "views" option145 parent.dirname = app.set('views') || process.cwd() + '/views';146147 // utilize "view engine" option148 if (app.set('view engine')) {149 parent.extension = '.' + app.set('view engine');150 }151152 var str = renderPartial(this, view, options, null, parent);153 this.send(str, options.headers, options.status);154};155156/**157 * Render `view` with the given `options` and optional callback `fn`.158 * When a callback function is given a response will _not_ be made159 * automatically, however otherwise a response of _200_ and _text/html_ is given.160 *
12 unmodified lines
173 * @param {Function} fn174 * @api public175 */176154http.ServerResponse.prototype.render = function(view, opts, fn, parent){177res.render = function(view, opts, fn, parent){178 // support callback function as second arg179 if (typeof opts === 'function') {180 fn = opts, opts = null;181 }
79 unmodified lines
261 union(options, helpers);262263 // Always expose partial() as a local264 options.partial = function(path, opts){242 return self.partial(path, opts, options, view);265 return renderPartial(self, path, opts, options, view);266 };267268 function error(err) {269 if (fn) {
32 unmodified lines
modified:test/view.test.js+32 added-24 removed
431 unmodified lines
432 433 'test #partial()': function(){434 var app = create();435436 app.set('view engine', 'jade');437438 // Auto-assigned local w/ collection option439 app.get('/', function(req, res){440 res.render('items.jade', { items: ['one', 'two'] });441 });
33 unmodified lines
475 { body: html });476477 // as: str collection option478 app.get('/user', function(req, res){477 res.send(res.partial('user.jade', {479 res.partial('user', {480 as: 'person',481 collection: [{ name: 'tj' }]480 }));482 });483 });484 485 assert.response(app,486 { url: '/user' },487 { body: '<p>tj</p>' });488489 // as: with object collection490 app.get('/user/object', function(req, res){489 res.send(res.partial('user.jade', {491 res.partial('user.jade', {492 as: 'person',493 collection: { 0: { name: 'tj' }, length: 1 }492 }));494 });495 });496 497 assert.response(app,498 { url: '/user' },499 { body: '<p>tj</p>' });500 501 // as: this collection option502 app.get('/person', function(req, res){501 res.send(res.partial('person.jade', {503 res.partial('person.jade', {504 as: this,505 collection: [{ name: 'tj' }],506 locals: { label: 'name:' }505 }));507 });508 });509 510 assert.response(app,511 { url: '/person' },512 { body: '<p>name: tj</p>' });513514 // as: global collection option515 app.get('/videos', function(req, res){514 res.send(res.partial('video.jade', {516 res.partial('video.jade', {517 as: global,518 collection: movies517 }));519 });520 });521522 assert.response(app,523 { url: '/videos' },524 { body: '<p>Tim Burton</p><p>James Cameron</p>' });525 526 // Magic variables527 app.get('/magic', function(req, res){526 res.send(res.partial('magic.jade', {528 res.partial('magic.jade', {529 as: 'word',530 collection: ['one', 'two', 'three']529 }));531 });532 });533 534 assert.response(app,535 { url: '/magic' },536 { body: '<li class="first">one</li><li class="word-1">two</li><li class="last">three</li>' });537 538 // Non-collection support539 app.get('/movie', function(req, res){538 res.send(res.partial('movie.jade', {540 res.partial('movie.jade', {541 object: movies[0]540 }));542 });543 });544 545 assert.response(app,546 { url: '/movie' },547 { body: '<li><div class="title">Nightmare Before Christmas</div><div class="director">Tim Burton</div></li>' });548 549 app.get('/video-global', function(req, res){548 res.send(res.partial('video.jade', {550 res.partial('video.jade', {551 object: movies[0],552 as: global551 })); 553 }); 554 });555 556 // Non-collection as: global557 assert.response(app,558 { url: '/video-global' },559 { body: '<p>Tim Burton</p>' });560561 app.get('/person-this', function(req, res){560 res.send(res.partial('person.jade', {562 res.partial('person.jade', {563 object: { name: 'tj' },564 locals: { label: 'User:' },565 headers: { 'Content-Type': 'text/html; utf-8' },566 status: 500,567 as: this564 })); 568 }); 569 });570 571 // Non-collection as: this572 assert.response(app,573 { url: '/person-this' },570 { body: '<p>User: tj</p>' });574 { body: '<p>User: tj</p>'575 , status: 500576 , headers: { 'Content-Type': 'text/html; utf-8' }});577578 // No options579 app.get('/nothing', function(req, res){574 res.send(res.partial('hello.ejs'));580 res.partial('hello.ejs');581 });582 583 assert.response(app,584 { url: '/nothing' },585 { body: 'Hello' });586587 // Path segments + "as"588 app.get('/role/as', function(req, res){583 res.send(res.partial('user/role.ejs', { as: 'role', collection: ['admin', 'member'] }));589 res.partial('user/role.ejs', { as: 'role', collection: ['admin', 'member'] });590 });591592 assert.response(app,593 { url: '/role/as' },594 { body: '<li>Role: admin</li><li>Role: member</li>' });595596 // Deduce name from last segment597 app.get('/role', function(req, res){592 res.send(res.partial('user/role.ejs', ['admin', 'member']));598 res.partial('user/role.ejs', ['admin', 'member']);599 });600601 assert.response(app,602 { url: '/role' },
26 unmodified lines
629 630 'test #partial() locals': function(){631 var app = create();632633 app.set('view engine', 'jade');634635 app.get('/', function(req, res, next){628 res.send(res.partial('pet-count.jade', {636 res.partial('pet-count', {637 locals: {638 pets: {639 length: 5640 }641 }634 }));642 });643 });644645 assert.response(app,646 { url: '/' },
18 unmodified lines
665 'test #partial() object': function(){666 var app = create();667668 app.get('/', function(req, res, next){661 res.send(res.partial('movie.jade', {669 res.partial('movie.jade', {670 title: 'Foobar'671 , director: 'Tim Burton'664 }));672 });673 });674675 assert.response(app,676 { url: '/' },
257 unmodified lines