Skip to content
Myr

view.js

This file

Path

lib/view/view.js

210 lines · 4.1 KiB · JavaScript

Last changed by

Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6

History of this file

Take it

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 1–137why, one step back
1
2/*!3 * Express - View4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var utils = require('../utils')13 , extname = utils.extname14 , dirname = utils.dirname15 , basename = utils.basename16 , fs = require('fs')17 , stat = fs.statSync;1819/**20 * Memory cache.21 */2223var cache = {};2425/**26 * Existance cache.27 */2829var exists = {};3031/**32 * Initialize a new `View` with the given `view` path and `options`.33 *34 * @param {String} view35 * @param {Object} options36 * @api private37 */3839var View = exports = module.exports = function View(view, options) {40 options = options || {};41 // TODO: more caching42 this.view = view;43 this.root = options.root;44 this.relative = false !== options.relative;45 this.defaultEngine = options.defaultEngine;46 this.parent = options.parentView;47 this.basename = basename(view);48 this.engine = this.resolveEngine();49 this.extension = '.' + this.engine;50 this.path = this.resolvePath();51 this.dirname = dirname(this.path);52};5354/**55 * Check if the view path exists.56 *57 * @return {Boolean}58 * @api public59 */6061View.prototype.__defineGetter__('exists', function(){62 var path = this.path;63 if (null != exists[path]) {64 return exists[path];65 } else {66 try {67 stat(path);68 return exists[path] = true;69 } catch (err) {70 return exists[path] = false;71 }72 }73});7475/**76 * Resolve view engine.77 *78 * @return {String}79 * @api private80 */8182View.prototype.resolveEngine = function(){83 // Explicit84 if (~this.basename.indexOf('.')) return extname(this.basename).substr(1);85 // Inherit from parent86 if (this.parent) return this.parent.engine;87 // Default88 return this.defaultEngine;89};9091/**92 * Resolve view path.93 *94 * @return {String}95 * @api private96 */9798View.prototype.resolvePath = function(){99 var path = this.view;100 // Implicit engine101 if (!~this.basename.indexOf('.')) path += this.extension;102 // Absolute103 if ('/' == path[0]) return path;104 // Relative to parent105 if (this.relative && this.parent) return this.parent.dirname + '/' + path;106 // Relative to root107 return this.root108 ? this.root + '/' + path109 : path;110};111112/**113 * Get view contents. This is a one-time hit, so we114 * can afford to be sync.115 *116 * @return {String}117 * @api public118 */119120View.prototype.__defineGetter__('contents', function(){121 return fs.readFileSync(this.path, 'utf8');122});123124/**125 * Get template engine api, cache exports to reduce126 * require() calls.127 *128 * @return {Object}129 * @api public130 */131132View.prototype.__defineGetter__('templateEngine', function(){133 var ext = this.extension;134 return cache[ext] || (cache[ext] = require(this.engine));135});136137/**

Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6

lines 138–149why, one step back
138 * Return root path alternative.
139 *140 * @return {String}141 * @api public142 */143144View.prototype.__defineGetter__('rootPath', function(){145 this.relative = false;146 return this.resolvePath();147});148149/**

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 150–162why, one step back
150 * Return index path alternative.
151 *152 * @return {String}153 * @api public154 */155156View.prototype.__defineGetter__('indexPath', function(){157 return this.dirname158 + '/' + this.basename.replace(this.extension, '')159 + '/index' + this.extension;160});161162/**

Added View#upIndexPathTj Holowaychukbefore adoption32be4a7

lines 163–173why, one step back
163 * Return ../index path alternative.
164 *165 * @return {String}166 * @api public167 */168169View.prototype.__defineGetter__('upIndexPath', function(){170 return this.dirname + '/index' + this.extension;171});172173/**

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 174–210why, one step back
174 * Return _ prefix path alternative
175 *176 * @return {String}177 * @api public178 */179180View.prototype.__defineGetter__('prefixPath', function(){181 return this.dirname + '/_' + this.basename;182});183184/**185 * Register the given template engine `exports`186 * as `ext`. For example we may wish to map ".html"187 * files to jade:188 *189 * app.register('.html', require('jade'));190 *191 * or192 *193 * app.register('html', require('jade'));194 *195 * This is also useful for libraries that may not196 * match extensions correctly. For example my haml.js197 * library is installed from npm as "hamljs" so instead198 * of layout.hamljs, we can register the engine as ".haml":199 *200 * app.register('.haml', require('haml-js'));201 *202 * @param {String} ext203 * @param {Object} obj204 * @api public205 */206207exports.register = function(ext, exports) {208 if ('.' != ext[0]) ext = '.' + ext;209 cache[ext] = exports;210};