Skip to content
Myr

Started re-write of views

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 64b24a9 · Browse files · 6 files+156 added−194 removed
This commit

State

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

Diff stats

6 files+156 added−194 removed

  • other:+156 added−194 removed

6 files+156 added−194 removed

modified:examples/chat/app.js+1 added
deleted:lib/express/middleware/view.js-194 removed
12// Express - View - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * Module dependencies.6 */78var Request = require('./../request').Request,9 extname = require('path').extname,10 fs = require('fs')1112/**13 * Cache supported template engine exports.14 */1516var engines = {}1718/**19 * View cache.20 */21 22var cache = { views: {}, partials: {} }232425/**26 * View helpers (merged with locals).27 */2829var helpers = exports.helpers = {}3031/**32 * Cache view files where _type_ is33 * "partials" or "views".34 *35 * @param {string} type36 * @api public37 */3839function cacheFiles(type) { 40 (function cacheDir(dir) {41 try {42 fs.readdirSync(dir).each(function(file){43 file = dir + '/' + file44 var stat = fs.statSync(file)45 if (stat.isDirectory() && file != set('partials')) 46 cacheDir(file)47 else if (stat.isFile())48 cache[type][file] = fs.readFileSync(file, 'utf8')49 })50 } catch (err) {51 if (err.errno !== process.ENOENT) throw e52 }53 })(set(type))54}5556// --- View5758exports.View = Plugin.extend({59 extend: {60 61 /**62 * Initialize extensions.63 */64 65 init: function() {66 67 // Settings68 69 if (!set('views'))70 set('views', function(){ return set('root') + '/views' })71 72 if (!set('partials'))73 set('partials', function(){ return set('views') + '/partials' })74 75 // Cache views in memory76 77 if (set('cache view contents'))78 cacheFiles('views')79 80 if (set('cache view partials'))81 cacheFiles('partials')82 83 // Request84 85 Request.include({86 87 /**88 * Render _view_ partial with _options_.89 * View Request#render() for additional options.90 *91 * Options:92 * - as: String name for the id used to which "collection" assign it's current value.93 * - collection: Array of objects, the name is derived from94 * the view name itself. For example 'video.haml.html'95 * will have an object "video" available to it.96 *97 * @param {string} view98 * @param {hash} options99 * @settings 'partials', 'cache view partials'100 * @return {string}101 * @api public102 */103 104 partial: function(view, options) {105 var options = options || {}106 options.partial = true107 options.layout = false108 if (options.collection) {109 var name = options.as || view.split('.').first,110 len = options.collection.length111 options.locals = options.locals || {}112 options.locals.__length__ = len113 return options.collection.map(function(val, i){114 options.locals.__isFirst__ = i === 0115 options.locals.__index__ = i116 options.locals.__isLast__ = i === len - 1117 options.locals[name] = val118 return this.render(view, options)119 }, this).join('')120 } else121 return this.render(view, options)122 },123124 /**125 * Render _view_ with _options_.126 *127 * Views are looked up relative to the'views' path setting. 128 * View filenames should conform to NAME.TYPE.ENGINE so for example129 * 'layout.html.ejs', 'ejs' represents the template engine, 'html'130 * represents the type of content being rendered, which is then passed131 * to contentType().132 *133 * Engines must export a render() method accepting the template string134 * and a hash of options. Engines can respond to the options listed below135 * as well as their own arbitrary ones. The "filename" option is always136 * passed as the path to the given _view_, allowing engines to perform137 * better error reporting.138 *139 * Options:140 *141 * - encoding: Passed to Request#respond()142 * - layout: The layout to use, none when falsey. Defaults to 'layout'143 * - locals: Most engines support a hash of local variable names / values.144 * - context: Most engines support an evaluation context (the 'this' keyword). 145 * Defaults to the current Request instance.146 *147 * Optionally you may also pass a _callback_ function which148 * will be called instead of responding with the 200 status code.149 *150 * @param {string} view151 * @param {object} options152 * @param {function} callback153 * @settings 'views', 'cache view contents'154 * @api public155 */156157 render: function(view, options, callback) {158 var options = options || {},159 type = options.partial ? 'partials' : 'views',160 path = set(type) + '/' + view,161 parts = view.split('.'),162 engine = parts.last,163 contentType = parts.slice(-2)[0],164 layout = options.layout === undefined165 ? 'layout'166 : options.layout167 options.locals = options.locals || {}168 Object.merge(options.locals, helpers)169 options.filename = path170 if (set('cache view contents'))171 options.cache = true172 var content = cache[type][path] || fs.readFileSync(path).toString(options.encoding || 'utf8')173 options.context = options.context || this174 content = (engines[engine] = engines[engine] || require(engine)).render(content, options)175 if (type === 'views') this.contentType(contentType)176 if (layout) {177 layout = layout.indexOf('.') !== -1178 ? layout179 : [layout, contentType, engine].join('.')180 this.render(layout, Object.mergeDeep(options, {181 layout: false,182 locals: { body: content }183 }), callback)184 } else if (type === 'partials')185 return content186 else if (callback)187 callback.call(this, null, content)188 else189 this.respond(200, content, options.encoding)190 }191 })192 }193 }194})
added:lib/express/view.js+149 added
12/*!3 * Express - View4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var extname = require('path').extname,13 mime = require('connect/utils').mime,14 http = require('http'),15 fs = require('fs');1617/**18 * Cache supported template engine exports.19 */2021var engines = {};2223/**24 * View cache.25 */26 27var cache = { views: {}, partials: {} };2829/**30 * View helpers (merged with locals).31 */3233var helpers = exports.helpers = {};3435/**36 * Render _view_ partial with _options_.37 *38 * Options:39 * - as: String name for the id used to which "collection" assign it's current value.40 * - collection: Array of objects, the name is derived from41 * the view name itself. For example 'video.haml.html'42 * will have an object "video" available to it.43 *44 * @param {String} view45 * @param {Object} options46 * @return {String}47 * @api public48 */4950http.ServerResponse.prototype.partial = function(view, options){51 options = options || {};52 options.partial = true;53 options.layout = false;54 if (options.collection) {55 var name = options.as || view.split('.')[0].56 len = options.collection.length;57 options.locals = options.locals || {};58 options.locals.__length__ = len;59 return options.collection.map(function(val, i){60 options.locals.__isFirst = i === 0;61 options.locals.__index = i;62 options.locals.__n = i + 1;63 options.locals.__isLast = i === len - 1;64 return this.render(view, options);65 }, this);66 } else {67 this.render(view, options);68 }69};7071/**72 * Render _view_ with _options_.73 *74 * Views are looked up relative to the'views' path setting. 75 * View filenames should conform to NAME.TYPE.ENGINE so for example76 * 'layout.html.ejs', 'ejs' represents the template engine, 'html'77 * represents the type of content being rendered, which is then passed78 * to contentType().79 *80 * Engines must export a render() method accepting the template string81 * and a hash of options. Engines can respond to the options listed below82 * as well as their own arbitrary ones. The "filename" option is always83 * passed as the path to the given _view_, allowing engines to perform84 * better error reporting.85 *86 * Options:87 *88 * - encoding: Passed to Request#respond()89 * - layout: The layout to use, none when falsey. Defaults to 'layout'90 * - locals: Most engines support a hash of local variable names / values.91 * - context: Most engines support an evaluation context (the 'this' keyword). 92 * Defaults to the current Request instance.93 *94 * Optionally you may also pass a _callback_ function which95 * will be called instead of responding with the 200 status code.96 *97 * @param {String} view98 * @param {Object} options99 * @param {Function} fn100 * @api public101 */102103http.ServerResponse.prototype.render = function(view, options){104 options = options || {};105 var type = options.partial ? 'partials' : 'views',106 path = set(type) + '/' + view,107 parts = view.split('.'),108 engine = parts[parts.length - 1],109 contentType = parts.slice(-2)[0],110 layout = options.layout === undefined ? 'layout' : options.layout;111112 // Default filename and evaluation context113 options.filename = path;114 options.context = options.context || this;115116 // Merge locals117 options.locals = options.locals || {};118 for (var key in helpers) {119 options.locals[key] = helpers[key];120 }121122 // View contents123 var content = cache[type][path] || fs.readFileSync(path).toString(options.encoding || 'utf8');124 125 // Render the view126 content = (engines[engine] = engines[engine] || require(engine)).render(content, options);127 128 // Set Content-Type129 if (type === 'views' && !this.headers['Content-Type']) {130 this.headers['Content-Type'] = mime.type(contentType);131 }132 133 // Layout support134 if (layout) {135 layout = layout.indexOf('.') > 0136 ? [layout. contentType, engine].join('.');137 // TODO: callback138 // TODO: deep merge options139 this.render(layout, {140 layout: false,141 locals: { body: content }142 });143 } else if (type === 'partials') {144 return content;145 // TODO: callback146 } else {147 this.writeHead(200, content, options.encoding);148 }149};
added:support/hamlnot shown

Its text is not shown, added.

added:support/sassnot shown

Its text is not shown, added.

modified:.gitmodules+6 added
17 unmodified lines
18 url = git://github.com/extjs/Connect.git19[submodule "support/expresso"]20 path = support/expresso21 url = git://github.com/visionmedia/expresso.git22[submodule "support/sass"]23 path = support/sass24 url = git://github.com/visionmedia/sass.js.git25[submodule "support/haml"]26 path = support/haml27 url = git://github.com/visionmedia/haml.js.git