Skip to content
Myr

View is now a plugin [#87]

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

authored by visionmedia · committed by visionmedia · parent a52845e · Browse files · 6 files+114 added−98 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+114 added−98 removed

  • tests:+16 added−18 removed
  • other:+98 added−80 removed

6 files+114 added−98 removed

added:lib/express/plugins/view.js+97 added
12// Express - View - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34var posix = require('posix')56/**7 * Template content cache.8 */910var cache = {}1112/**13 * Support template engines.14 */1516var engine = {17 ejs: require('support/ejs'),18 haml: require('support/haml')19}2021// --- View2223exports.View = Plugin.extend({24 extend: {25 26 /**27 * Initialize extensions.28 */29 30 init: function() {31 32 // Settings33 34 set('views', function(){ return set('root') + '/views' })35 36 // Request37 38 Request.include({3940 /**41 * Render _view_ with _options_.42 *43 * Views are looked up relative to the'views' path setting. 44 * View filenames should conform to ANY.ENGINE.TYPE so for example45 * 'layout.ejs.html', 'ejs' represents the template engine, 'html'46 * represents the type of content being rendered, which is then passed47 * to contentType().48 *49 * Engines must export a render() method accepting the template string50 * and a hash of options.51 *52 * Options:53 *54 * - layout: Whether or not to use a layout. Defaults to true55 * - context: Most engines support an evaluation context (the 'this' keyword)56 * - locals: Most engines support a hash of local variable names / values.57 *58 * @param {string} view59 * @param {hash} options60 * @settings 'views', 'cache views'61 * @api public62 */6364 render: function(view, options) {65 var self = this,66 options = options || {},67 path = set('views') + '/' + view,68 type = path.split('.').slice(-2)[0],69 ext = extname(path),70 layout = options.layout === undefined ? true : options.layout71 self.contentType(ext)72 function render(content) {73 content = engine[type].render(content, options)74 if (layout)75 self.render('layout.' + type + '.' + ext, process.mixin(options, {76 layout: false,77 locals: {78 body: content79 }80 }))81 else82 self.halt(200, content)83 }84 if (set('cache views') && cache[view])85 render(cache[view])86 else87 posix88 .cat(path)89 .addCallback(function(content){90 render(cache[view] = content)91 })92 }93 })94 }95 }96})97
modified:lib/express/core.js+1 added-2 removed
7 unmodified lines
8process.mixin(require('express/request'))9process.mixin(require('express/helpers'))10process.mixin(require('express/plugin'))11process.mixin(require('express/mime'))12process.mixin(require('express/view'))12process.mixin(require('express/dsl'))1314// --- Route15
238 unmodified lines
254255// --- Defaults256257configure(function(){259 set('views', function(){ return set('root') + '/views' })258 use(require('express/plugins/view').View)259 use(require('express/plugins/body-decoder').BodyDecoder)260})261262configure('development', function(){
7 unmodified lines
deleted:lib/express/view.js-78 removed
modified:spec/spec.node.js+1 added-1 removed
added:spec/spec.plugins.view.js+15 added
deleted:spec/spec.view.js-17 removed