本文整理汇总了TypeScript中nunjucks.Environment类的典型用法代码示例。如果您正苦于以下问题:TypeScript Environment类的具体用法?TypeScript Environment怎么用?TypeScript Environment使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Environment类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: callsite
function callsite(): any {
const orig = Error.prepareStackTrace;
Error.prepareStackTrace = (_, traces) => traces;
const err = new Error();
Error.captureStackTrace(err, callsite);
const stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
}
const REG_HTML = /(<([^>]+)>)/gi;
const REG_ATTR = /\s?(style|class|width|height)=".+?"/g;
const REG_SCRIPT = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
const env = new Environment(new FileSystemLoader('.', {
noCache: process.env.NODE_ENV !== 'production',
}));
// remove HTML tags
env.addFilter('rmhtml', (content: string) => content.replace(REG_HTML, ''));
// remove HTML attr
env.addFilter('rmattr', (content: string) => content.replace(REG_ATTR, ''));
// remove SCRIPT tag
env.addFilter('rmxss', (content: string) => content.replace(REG_SCRIPT, ''));
// moment format date
env.addFilter('date', (time: any, format: string) => time && moment(time).format(format));
export default function (ctx: Context, next: () => Promise<any>) {
示例2: resolve
ctx.render = (path: string, viewModel?: any) => {
// 本地路径 (相对调用目录)
if (!path.startsWith('/')) {
path = resolve(callsite()[1].getFileName(), '../', path);
}
return env.render(path, {
...viewModel,
STATIC_PREFIX: process.env.STATIC_PREFIX!.replace(/\/$/, ''),
});
};
示例3:
extensions.forEach(ext => this.env.addExtension(ext.name, ext.ext));
示例4: Promise
return new Promise((resolve, reject) => {
this.env.renderString(shell, context, (err, res) => {
if (err) return reject(err);
return resolve(res);
});
});