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
Provenance
- Commit
e1da383- Tree
8a9dca0- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
6bcf291
3 files changed+71 added−36 removed
modified:lib/application.js+24 added-35 removed
12 unmodified lines13 , 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 lines541 542 app.del = app.delete;543 544 /**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 */554 555-app.render = function(view, options, fn){555+app.render = function(name, options, fn){556 var self = this557 , opts = {}558 , cache = this.cache559- , engines = this.engines;559+ , engines = this.engines560+ , view;561 562 // support callback function as second arg563 if ('function' == typeof options) {564 fn = options, options = {};10 unmodified lines575 576 // merge options577 utils.merge(opts, options);578 578- // 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;583 582- // pass .cache to the engine583- opts.cache = this.enabled('view cache');584+ // primed cache585+ if (opts.cache) view = cache[name];586 585- // 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+ });594 595+ // prime the cache596+ if (opts.cache) cache[name] = view;597+ }598+599 // 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 };603-604-/**605- * Lookup the given `view` and callback `fn(err, path)`.606- *607- * @param {String} view608- * @param {String} fn609- * @api public610- */611-612-app.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
1233456787 unmodified lines
/*! * Express - Utils * Express - utils * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca> * MIT Licensed */
87 unmodified linesadded:lib/view.js+46 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
/*! * Express - View * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca> * MIT Licensed */
/** * Module dependencies. */
var path = require('path') , fs = require('fs') , extname = path.extname , join = path.join;
/** * Expose `View`. */
module.exports = View;
/** * Initialize a new `View` with the given `name`. * * @param {String} name * @param {Object} options * @api public */
function View(name, options) { options = options || {}; this.name = name; var engines = options.engines; this.defaultEngine = options.defaultEngine; this.extname = extname(name); if (!this.extname) name += '.' + (this.extname = this.defaultEngine); this.engine = engines[this.extname] || (engines[this.extname] = require(this.extname)); this.path = join(options.root, name); this.string = fs.readFileSync(this.path, 'utf8');}
View.prototype.render = function(options, fn){ options.filename = this.path; this.engine.render(this.string, options, fn);};