You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
3.1 KiB

  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. opts.engines[this.ext] = require(this.ext.substr(1)).__express;
  64. }
  65. // store loaded engine
  66. this.engine = opts.engines[this.ext];
  67. // lookup path
  68. this.path = this.lookup(fileName);
  69. }
  70. /**
  71. * Lookup view by the given `name`
  72. *
  73. * @param {string} name
  74. * @private
  75. */
  76. View.prototype.lookup = function lookup(name) {
  77. var path;
  78. var roots = [].concat(this.root);
  79. debug('lookup "%s"', name);
  80. for (var i = 0; i < roots.length && !path; i++) {
  81. var root = roots[i];
  82. // resolve the path
  83. var loc = resolve(root, name);
  84. var dir = dirname(loc);
  85. var file = basename(loc);
  86. // resolve the file
  87. path = this.resolve(dir, file);
  88. }
  89. return path;
  90. };
  91. /**
  92. * Render with the given options.
  93. *
  94. * @param {object} options
  95. * @param {function} callback
  96. * @private
  97. */
  98. View.prototype.render = function render(options, callback) {
  99. debug('render "%s"', this.path);
  100. this.engine(this.path, options, callback);
  101. };
  102. /**
  103. * Resolve the file within the given directory.
  104. *
  105. * @param {string} dir
  106. * @param {string} file
  107. * @private
  108. */
  109. View.prototype.resolve = function resolve(dir, file) {
  110. var ext = this.ext;
  111. // <path>.<ext>
  112. var path = join(dir, file);
  113. var stat = tryStat(path);
  114. if (stat && stat.isFile()) {
  115. return path;
  116. }
  117. // <path>/index.<ext>
  118. path = join(dir, basename(file, ext), 'index' + ext);
  119. stat = tryStat(path);
  120. if (stat && stat.isFile()) {
  121. return path;
  122. }
  123. };
  124. /**
  125. * Return a stat, maybe.
  126. *
  127. * @param {string} path
  128. * @return {fs.Stats}
  129. * @private
  130. */
  131. function tryStat(path) {
  132. debug('stat "%s"', path);
  133. try {
  134. return fs.statSync(path);
  135. } catch (e) {
  136. return undefined;
  137. }
  138. }