view.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*!
  2. * express
  3. * Copyright(c) 2009-2013 TJ Holowaychuk
  4. * Copyright(c) 2013 Roman Shtylman
  5. * Copyright(c) 2014-2015 Douglas Christopher Wilson
  6. * MIT Licensed
  7. */
  8. 'use strict';
  9. /**
  10. * Module dependencies.
  11. * @private
  12. */
  13. var debug = require('debug')('express:view');
  14. var path = require('path');
  15. var fs = require('fs');
  16. var utils = require('./utils');
  17. /**
  18. * Module variables.
  19. * @private
  20. */
  21. var dirname = path.dirname;
  22. var basename = path.basename;
  23. var extname = path.extname;
  24. var join = path.join;
  25. var resolve = path.resolve;
  26. /**
  27. * Module exports.
  28. * @public
  29. */
  30. module.exports = View;
  31. /**
  32. * Initialize a new `View` with the given `name`.
  33. *
  34. * Options:
  35. *
  36. * - `defaultEngine` the default template engine name
  37. * - `engines` template engine require() cache
  38. * - `root` root path for view lookup
  39. *
  40. * @param {string} name
  41. * @param {object} options
  42. * @public
  43. */
  44. function View(name, options) {
  45. var opts = options || {};
  46. this.defaultEngine = opts.defaultEngine;
  47. this.ext = extname(name);
  48. this.name = name;
  49. this.root = opts.root;
  50. if (!this.ext && !this.defaultEngine) {
  51. throw new Error('No default engine was specified and no extension was provided.');
  52. }
  53. var fileName = name;
  54. if (!this.ext) {
  55. // get extension from default engine name
  56. this.ext = this.defaultEngine[0] !== '.'
  57. ? '.' + this.defaultEngine
  58. : this.defaultEngine;
  59. fileName += this.ext;
  60. }
  61. if (!opts.engines[this.ext]) {
  62. // load engine
  63. var mod = this.ext.substr(1)
  64. debug('require "%s"', mod)
  65. opts.engines[this.ext] = require(mod).__express
  66. }
  67. // store loaded engine
  68. this.engine = opts.engines[this.ext];
  69. // lookup path
  70. this.path = this.lookup(fileName);
  71. }
  72. /**
  73. * Lookup view by the given `name`
  74. *
  75. * @param {string} name
  76. * @private
  77. */
  78. View.prototype.lookup = function lookup(name) {
  79. var path;
  80. var roots = [].concat(this.root);
  81. debug('lookup "%s"', name);
  82. for (var i = 0; i < roots.length && !path; i++) {
  83. var root = roots[i];
  84. // resolve the path
  85. var loc = resolve(root, name);
  86. var dir = dirname(loc);
  87. var file = basename(loc);
  88. // resolve the file
  89. path = this.resolve(dir, file);
  90. }
  91. return path;
  92. };
  93. /**
  94. * Render with the given options.
  95. *
  96. * @param {object} options
  97. * @param {function} callback
  98. * @private
  99. */
  100. View.prototype.render = function render(options, callback) {
  101. debug('render "%s"', this.path);
  102. this.engine(this.path, options, callback);
  103. };
  104. /**
  105. * Resolve the file within the given directory.
  106. *
  107. * @param {string} dir
  108. * @param {string} file
  109. * @private
  110. */
  111. View.prototype.resolve = function resolve(dir, file) {
  112. var ext = this.ext;
  113. // <path>.<ext>
  114. var path = join(dir, file);
  115. var stat = tryStat(path);
  116. if (stat && stat.isFile()) {
  117. return path;
  118. }
  119. // <path>/index.<ext>
  120. path = join(dir, basename(file, ext), 'index' + ext);
  121. stat = tryStat(path);
  122. if (stat && stat.isFile()) {
  123. return path;
  124. }
  125. };
  126. /**
  127. * Return a stat, maybe.
  128. *
  129. * @param {string} path
  130. * @return {fs.Stats}
  131. * @private
  132. */
  133. function tryStat(path) {
  134. debug('stat "%s"', path);
  135. try {
  136. return fs.statSync(path);
  137. } catch (e) {
  138. return undefined;
  139. }
  140. }