Started implementing partials again
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 77f4cd5 · Browse files · 4 files·+41 added−14 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+41 added−14 removed
- tests:+13 added−0 removed
- other:+28 added−14 removed
Provenance
- Commit
4c0704e- Tree
f264bef- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
77f4cd5
4 files changed+41 added−14 removed
modified:lib/express/view.js+28 added-14 removed
31 unmodified lines323334353636373839403940414243445 unmodified lines50515253545455565556575858596061626359606162636465656667676869707119 unmodified lines91929394959697989910010110210310410510610710810911011111211311411511611711811912014 unmodified lines31 unmodified lines
var helpers = exports.helpers = {};
/** * Render _view_ partial with _options_. * Render `view` partial with the given `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 * - `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} view5 unmodified lineshttp.ServerResponse.prototype.partial = function(view, options){ options = options || {}; options.partial = true; options.layout = false; var collection = options.collection; if (options.collection) { var name = options.as || view.split('.')[0]. len = options.collection.length; if (collection) { var name = options.as || view.split('.')[0], len = collection.length; options.locals = options.locals || {}; options.locals.__length__ = len; return options.collection.map(function(val, i){ options.locals.__isFirst = i === 0; options.locals.__index = i; options.locals.__n = i + 1; options.locals.__isLast = i === len - 1; options.locals.collectionLength = len; return collection.map(function(val, i){ options.locals.firstInCollection = i === 0; options.locals.indexInCollection = i; options.locals.lastInCollection = i === len - 1; return this.render(view, options); }, this); }, this).join(''); } else { this.render(view, options); return this.render(view, options); }};
/**19 unmodified lines layout = layout === true ? 'layout' + ext : layout;
// Always expose partial() as a local options.locals = options.locals || {}; options.locals.partial = function(){ self.partial.apply(self, arguments); };
// Partials support if (options.partial) { root += '/partials'; }
// Send response function send(str) { self.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': str.length }); self.end(str); } // Pass filename to the engine options.filename = path;
// Render fs.readFile(path, 'utf8', function(err, str){ // TODO: error handling14 unmodified linesadded:test/fixtures/partials/item.jade+1 added
1+li= item.nameadded:test/fixtures/items.jade+1 added
1+ul!= partial('item.jade', { collection: items })modified:test/view.test.js+11 added
35 unmodified lines363738394041424344454647484950515235 unmodified lines }); assert.response(app, { url: '/' }, { body: '<cool><p>Welcome</p></cool>' }); }, 'test #partial()': function(assert){ var app = express.createServer(); app.set('views', __dirname + '/fixtures'); app.get('/', function(req, res){ res.render('items.jade', { layout: false, locals: { items: ['one', 'two'] }}); }); assert.response(app, { url: '/' }, { body: '<ul><li>one</li><li>two</li></ul>' }); }};No newline at end of fileNo newline at end of file