Skip to content
Myr

view.js

This file

Path

lib/view.js

95 lines · 1.9 KiB · JavaScript

Last changed by

map register functionTj Holowaychukbefore adoption02f48aa

History of this file

Take it

new ViewTj Holowaychukbefore adoptione1da383

lines 1–13why, 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 path = require('path')13 , fs = require('fs')

added index lookup support backTj Holowaychukbefore adoption041974e

lines 14–15why, one step back
14 , dirname = path.dirname
15 , basename = path.basename

new ViewTj Holowaychukbefore adoptione1da383

line 16why, one step back
16 , extname = path.extname

added index lookup support backTj Holowaychukbefore adoption041974e

line 17why, one step back
17 , exists = path.existsSync

new ViewTj Holowaychukbefore adoptione1da383

lines 18–28why, one step back
18 , join = path.join;
1920/**21 * Expose `View`.22 */2324module.exports = View;2526/**27 * Initialize a new `View` with the given `name`.28 *

docsTj Holowaychukbefore adoption7369d86

lines 29–34why, one step back
29 * Options:
30 *31 * - `defaultEngine` the default template engine name 32 * - `engines` template engine require() cache 33 * - `root` root path for view lookup 34 *

new ViewTj Holowaychukbefore adoptione1da383

lines 35–36why, one step back
35 * @param {String} name
36 * @param {Object} options

docsTj Holowaychukbefore adoption7369d86

line 37why, one step back
37 * @api private

new ViewTj Holowaychukbefore adoptione1da383

lines 38–42why, one step back
38 */
3940function View(name, options) {41 options = options || {};42 this.name = name;

added index lookup support backTj Holowaychukbefore adoption041974e

line 43why, one step back
43 this.root = options.root;

new ViewTj Holowaychukbefore adoptione1da383

lines 44–45why, one step back
44 var engines = options.engines;
45 this.defaultEngine = options.defaultEngine;

misc refactoringTj Holowaychukbefore adoption705cdb6

line 46why, one step back
46 var ext = this.ext = extname(name);

map register functionTj Holowaychukbefore adoption02f48aa

lines 47–48why, one step back
47 if (!ext) name += (ext = this.ext = '.' + this.defaultEngine);
48 this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).render);

added index lookup support backTj Holowaychukbefore adoption041974e

line 49why, one step back
49 this.path = this.lookup(name);

View#contents()Tj Holowaychukbefore adoption6d47011

line 50why, one step back
50 this.string = this.contents();

new ViewTj Holowaychukbefore adoptione1da383

line 51why, one step back
51}

View#contents()Tj Holowaychukbefore adoption6d47011

lines 52–62why, one step back
52
53/**54 * Return the contents of this view.55 *56 * @return {String}57 * @api private58 */5960View.prototype.contents = function(){61 return fs.readFileSync(this.path, 'utf8');62};

added index lookup support backTj Holowaychukbefore adoption041974e

line 63why, one step back
63

docsTj Holowaychukbefore adoption90fc80f

lines 64–72why, one step back
64/**
65 * Lookup view by the given `path`66 *67 * @param {String} path68 * @return {String}69 * @api private70 */7172View.prototype.lookup = function(path){

map register functionTj Holowaychukbefore adoption02f48aa

line 73why, one step back
73 var ext = this.ext;

more refactoringTj Holowaychukbefore adoption371b381

line 74why, one step back
74

added index lookup support backTj Holowaychukbefore adoption041974e

line 75why, one step back
75 // <path>.<engine>

docsTj Holowaychukbefore adoption90fc80f

line 76why, one step back
76 path = join(this.root, path);

added index lookup support backTj Holowaychukbefore adoption041974e

lines 77–79why, one step back
77 if (exists(path)) return path;
7879 // <path>/index.<engine>

more refactoringTj Holowaychukbefore adoption371b381

line 80why, one step back
80 path = join(dirname(path), basename(path, ext), 'index' + ext);

added index lookup support backTj Holowaychukbefore adoption041974e

lines 81–82why, one step back
81 if (exists(path)) return path;
82};

docsTj Holowaychukbefore adoption7369d86

lines 83–90why, one step back
83
84/**85 * Render with the given `options` and callback `fn(err, str)`.86 *87 * @param {Object} options88 * @param {Function} fn89 * @api private90 */

new ViewTj Holowaychukbefore adoptione1da383

lines 91–93why, one step back
91
92View.prototype.render = function(options, fn){93 options.filename = this.path;

map register functionTj Holowaychukbefore adoption02f48aa

line 94why, one step back
94 this.engine(this.string, options, fn);

new ViewTj Holowaychukbefore adoptione1da383

line 95why, one step back
95};