Skip to content
Myr

new View

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 6bcf291 · Browse files · 3 files+71 added−36 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+71 added−36 removed

  • other:+71 added−36 removed

3 files+71 added−36 removed

modified:lib/application.js+24 added-35 removed
12 unmodified lines
13 , Router = require('./router')14 , toArray = require('./utils').toArray15 , methods = Router.methods.concat('del', 'all')16 , res = require('./response')17 , View = require('./view')18 , url = require('url')19 , utils = connect.utils20 , path = require('path')20 , extname = path.extname21 , join = path.join22 , fs = require('fs')23 , qs = require('qs');24
516 unmodified lines
541542app.del = app.delete;543544/**545 * Render the given `view` name with `options`545 * Render the given view `name` name with `options`546 * and a callback accepting an error and the547 * rendered template string.548 *549 * @param {String} view549 * @param {String} name550 * @param {String|Function} options or fn551 * @param {Function} fn552 * @api public553 */554555app.render = function(view, options, fn){555app.render = function(name, options, fn){556 var self = this557 , opts = {}558 , cache = this.cache559 , engines = this.engines;559 , engines = this.engines560 , view;561562 // support callback function as second arg563 if ('function' == typeof options) {564 fn = options, options = {};
10 unmodified lines
575576 // merge options577 utils.merge(opts, options);578578 // join "view engine" if necessary579 var ext = extname(view);580 if (!ext) view += '.' + (ext = this.set('view engine'));579 // set .cache unless explicitly provided580 opts.cache = null == opts.cache581 ? this.enabled('view cache')582 : opts.cache;583582 // pass .cache to the engine583 opts.cache = this.enabled('view cache');584 // primed cache585 if (opts.cache) view = cache[name];586585 // when no extension nor "view engine" is given586 if (!ext) return fn(new Error('failed to find view "' + view + '"'));587 // view588 if (!view) {589 view = new View(name, {590 defaultEngine: this.set('view engine')591 , root: this.set('views') || process.cwd() + '/views'592 , engines: engines593 });594595 // prime the cache596 if (opts.cache) cache[name] = view;597 }598599 // render600 try {590 var engine = engines[ext] = engines[ext] || require(ext);591 this.lookup(view, function(err, path){592 if (err) return fn(err);593 opts.filename = path;594 view = opts.cache595 ? cache[path] || (cache[path] = fs.readFileSync(path, 'utf8'))596 : fs.readFileSync(path, 'utf8');597 engine.render(view, opts, fn);598 });601 view.render(opts, fn);602 } catch (err) {603 fn(err);604 }605};603604/**605 * Lookup the given `view` and callback `fn(err, path)`.606 *607 * @param {String} view608 * @param {String} fn609 * @api public610 */611612app.lookup = function(view, fn){613 var root = this.set('views') || process.cwd() + '/views';614 view = join(root, view);615 fn(null, view);616};
modified:lib/utils.js+1 added-1 removed
added:lib/view.js+46 added