Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let files = _fs.lsfile(this.config.outputRoot, (fileName, file) => {
logger.log('debug', {
message: `compile template ${file}`
});
// 有些文件如果有乱码,会导致编译异常
try {
templates[file] = swig.compileFile(file, {autoescape: false});
// 如果 tokens.length === 1, 则说明没有需要替换的内容, 返回 false 以过滤掉该文件
return templates[file].tokens.length !== 1;
} catch (err) {
logger.log('error', {
message: `compile template error: ${err}`
});
}
return false;
});
function(name,file){
this.emit('debug',{
data:[file],
message:'complie template %s'
});
this._templates[file] = swig.compileFile(file,{autoescape:!1});
}
.bind(this)
exports.render = function(template, data) {
init();
if (!/\.html$/.test(template)) {
template = template + '.html';
}
var tpl = swig.compileFile(template);
data = _.defaults(data, store.globals);
return tpl.render(data);
};
var render = function(fpath, json) {
var tpl = cache[fpath];
if (tpl === undefined) {
var lfpath = public + fpath;
tpl = swig.compileFile(lfpath);
cache[fpath] = tpl;
}
return tpl(json);
};
render.text = function(string, json) {
fs.stat(path, function(err, stat) {
if (err)
self.status(500).end('Server Internal Error: ' + JSON.stringify(err));
else {
cacheTime[file] = stat.mtime;
cacheRender[file] = swig.compileFile(path);
self.end(cacheRender[file](json));
}
});
}
var render = function(fpath, json) {
var tpl;
fpath = root + fpath;
if (tpl !== undefined) {
tpl = cache[fpath];
} else {
tpl = swig.compileFile(fs.readFileSync(fpath));
cache[fpath] = tpl;
}
return tpl(json);
};
return through.obj(function(file, enc, cb) {
var data = {
site: site,
page: file.page,
content: file.contents.toString()
};
var templateFile = path.join(
__dirname, 'docs', '_templates', file.page.layout + '.html');
var tpl = swig.compileFile(templateFile, {cache: false});
file.contents = new Buffer(tpl(data), 'utf8');
this.push(file);
cb();
});
}
return through.obj(function(file, enc, cb) {
var data = {
site: site,
page: file.page,
content: file.contents.toString()
};
var templateFile = path.join(
__dirname, 'docs', '_templates', file.page.layout + '.html');
var tpl = swig.compileFile(templateFile, {cache: false});
file.contents = new Buffer(tpl(data), 'utf8');
this.push(file);
cb();
});
}
function applyTemplate(templateFile) {
var tpl = swig.compileFile(path.join(__dirname, templateFile));
return through.obj(function (file, enc, cb) {
var data = {
site: site,
page: file.page,
content: file.contents.toString()
};
file.contents = new Buffer(tpl(data), 'utf8');
this.push(file);
cb();
});
}
module.exports = function(layout, data){
var template = swig.compileFile('../layout/' + layout + '.html');
return template.render(data);
};