UNPKG

@ackplus/nest-dynamic-templates

Version:

A powerful and flexible dynamic template rendering library for NestJS applications with support for Nunjucks, Handlebars, EJS, Pug, MJML, Markdown, and more.

59 lines 1.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NunjucksEngine = void 0; const template_types_1 = require("../../interfaces/template.types"); const template_engine_1 = require("../template-engine"); const load_peer_1 = require("../load-peer"); class NunjucksEngine extends template_engine_1.TemplateEngine { options; customOptions; static engineName = template_types_1.TemplateEngineEnum.NUNJUCKS; static displayName = 'Nunjucks'; static peerPackage = 'nunjucks'; env; constructor(options = {}, customOptions = {}) { super(); this.options = options; this.customOptions = customOptions; } async getEnv() { if (this.env) return this.env; const nunjucks = await (0, load_peer_1.loadPeer)('nunjucks', 'njk', 'template'); const env = nunjucks.configure({ autoescape: true, throwOnUndefined: true, ...this.options, }); for (const [name, filter] of Object.entries(this.customOptions.filters ?? {})) { env.addFilter(name, filter); } for (const [name, value] of Object.entries(this.customOptions.globals ?? {})) { env.addGlobal(name, value); } this.env = env; return env; } async render(content, data) { const env = await this.getEnv(); return new Promise((resolve, reject) => { env.renderString(content, data || {}, (err, result) => { if (err) reject(err); else resolve(result ?? ''); }); }); } async validate(content) { try { await this.render(content, {}); return true; } catch { return false; } } } exports.NunjucksEngine = NunjucksEngine; //# sourceMappingURL=nunjucks.engine.js.map