gpt4 book ai didi

node.js - Swig-模板默认扩展名

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:55 25 4
gpt4 key购买 nike

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

 .html or .htm 

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

 .xml

最佳答案

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

关于node.js - Swig-模板默认扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25787086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com