@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.
48 lines • 1.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HandlebarsEngine = 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 HandlebarsEngine extends template_engine_1.TemplateEngine {
options;
customOptions;
static engineName = template_types_1.TemplateEngineEnum.HANDLEBARS;
static displayName = 'Handlebars';
static peerPackage = 'handlebars';
instance;
constructor(options = {}, customOptions = {}) {
super();
this.options = options;
this.customOptions = customOptions;
}
async getInstance() {
if (this.instance)
return this.instance;
const mod = await (0, load_peer_1.loadPeer)('handlebars', 'hbs', 'template');
const Handlebars = mod.default ?? mod;
const instance = typeof Handlebars.create === 'function' ? Handlebars.create() : Handlebars;
for (const [name, fn] of Object.entries(this.customOptions.filters ?? {})) {
instance.registerHelper(name, fn);
}
this.instance = instance;
return instance;
}
async render(content, data) {
const Handlebars = await this.getInstance();
const template = Handlebars.compile(content, this.options);
return template({ ...this.customOptions.globals, ...data });
}
async validate(content) {
try {
const Handlebars = await this.getInstance();
Handlebars.precompile(content, this.options);
return true;
}
catch {
return false;
}
}
}
exports.HandlebarsEngine = HandlebarsEngine;
//# sourceMappingURL=handlebars.engine.js.map