本文整理匯總了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);
});
});