Node.js Swig模板默认扩展名

Node.js Swig模板默认扩展名,node.js,swig-template,Node.js,Swig Template,我可以设置分机号码吗?例如: .html or .htm 我可以为一些布局设置自定义扩展吗?比如: .xml Swig不关心/不了解扩展。你可以试着写一封信来帮你做这件事。只需复制文件系统加载程序,让它检查给定的路径是否包含扩展,如果包含扩展,则使用默认路径 var fs = require('fs'), path = require('path'); module.exports = function (basepath, encoding) { var ret = {};

我可以设置分机号码吗?例如:

 .html or .htm 
我可以为一些布局设置自定义扩展吗?比如:

 .xml

Swig不关心/不了解扩展。你可以试着写一封信来帮你做这件事。只需复制文件系统加载程序,让它检查给定的路径是否包含扩展,如果包含扩展,则使用默认路径

var fs = require('fs'),
  path = require('path');

module.exports = function (basepath, encoding) {
  var ret = {};

  encoding = encoding || 'utf8';
  basepath = (basepath) ? path.normalize(basepath) : null;

  ret.resolve = function (to, from) {
    if (basepath) {
      from = basepath;
    } else {
      from = (from) ? path.dirname(from) : process.cwd();
    }
    return path.resolve(from, to);
  };

  ret.load = function (identifier, cb) {
    if (!fs || (cb && !fs.readFile) || !fs.readFileSync) {
      throw new Error('Unable to find file ' + identifier + ' because there is no filesystem to read from.');
    }

    // Start of added code...
    var extension = path.extname(identifier);

    if (!extension) {
      // Set this to whatever you want as a default
      // If the extension exists, like 'foo.xml', it won't add the '.html'
      identifier += '.html';
    }
    // End of added code

    identifier = ret.resolve(identifier);

    if (cb) {
      fs.readFile(identifier, encoding, cb);
      return;
    }
    return fs.readFileSync(identifier, encoding);
  };

  return ret;
};