【问题标题】:Swig-template default extensionSwig-template 默认扩展
【发布时间】:2014-09-11 12:08:40
【问题描述】:

我可以设置什么扩展名吗?例如:

 .html or .htm 

我可以为某些布局设置自定义扩展吗?喜欢:

 .xml

【问题讨论】:

    标签: node.js swig-template


    【解决方案1】:

    Swig 不关心/不知道扩展。你可以尝试写一个custom loader 来为你做这件事。只需复制文件系统加载器并让它检查给定的路径是否不包含扩展名,如果包含,请使用您的默认值。

    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;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2017-06-28
      • 2016-01-16
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多