Skip to content

Commit

Permalink
re-working view system
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Oct 7, 2011
1 parent ce03cf4 commit 7710db4
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 646 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ lib-cov
*.swp
*.swo
benchmarks/graphs
testing.js
testing
node_modules/
testing
30 changes: 27 additions & 3 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ app.__proto__ = connect.HTTPServer.prototype;
app.init = function(middleware){
var self = this;
this.cache = {};
this.engines = {};
this.settings = {};
this.redirects = {};
this.isCallbacks = {};
Expand Down Expand Up @@ -278,9 +279,32 @@ app.mounted = function(fn){
* @api public
*/

app.register = function(){
view.register.apply(this, arguments);
return this;
/**
* Register the given template engine `exports`
* as `ext`. For example we may wish to map ".html"
* files to jade:
*
* app.register('.html', require('jade'));
*
* or
*
* app.register('html', require('jade'));
*
* This is also useful for libraries that may not
* match extensions correctly. For example my haml.js
* library is installed from npm as "hamljs" so instead
* of layout.hamljs, we can register the engine as ".haml":
*
* app.register('.haml', require('haml-js'));
*
* @param {String} ext
* @param {Object} obj
* @api public
*/

app.register = function(ext, exports) {
if ('.' != ext[0]) ext = '.' + ext;
this.engines[ext] = exports;
};

/**
Expand Down
54 changes: 54 additions & 0 deletions lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ var connect = require('connect')
, toArray = require('./utils').toArray
, methods = router.methods.concat('del', 'all')
, res = require('./response')
, union = require('./utils').union
, url = require('url')
, utils = connect.utils
, path = require('path')
, extname = path.extname
, join = path.join
, fs = require('fs')
, qs = require('qs');

/**
Expand Down Expand Up @@ -530,3 +535,52 @@ app.all = function(path){

app.del = app.delete;

/**
* Render the given `view` name with `opts`
* and a callback accepting an error and the
* rendered template string.
*
* @param {String} view
* @param {String|Function} opts or fn
* @param {Function} fn
* @api public
*/

app.render = function(view, opts, fn){
var self = this
, options = {}
, cache = this.cache
, engines = this.engines
, root = this.set('views') || process.cwd() + '/views';

// support callback function as second arg
if ('function' == typeof opts) {
fn = opts, opts = null;
}

// merge app.locals
union(options, this.locals);

// merge render() options
if (opts) utils.merge(options, opts);

// join "view engine" if necessary
var ext = extname(view);
if (!ext) view += '.' + (ext = this.set('view engine'));

// pass .cache to the engine
options.cache = this.enabled('view cache');

// when no extension nor "view engine" is given
if (!ext) return fn(new Error('failed to find view "' + view + '"'));

// render
try {
var engine = cache[ext] = cache[ext] || require(ext);
options.filename = join(root, view);
view = fs.readFileSync(options.filename, 'utf8');
engine.render(view, options, fn);
} catch (err) {
fn(err);
}
};
Loading

0 comments on commit 7710db4

Please sign in to comment.