UNPKG

@veams/plugin-templater

Version:

A simple frontend template plugin which supports handlebars.

114 lines 4.01 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var Veams = {}; /** * Represents the Templater class which will be used in VeamsTemplater plugin. * @module Templater * * @author Sebastian Fitzner */ var Templater = /** @class */ (function () { function Templater(VEAMS, _a) { if (VEAMS === void 0) { VEAMS = window['Veams']; } var engine = _a.engine, templates = _a.templates, _b = _a.partials, partials = _b === void 0 ? null : _b, _c = _a.helpers, helpers = _c === void 0 ? null : _c; this.options = { engine: null, templates: null, partials: null, helpers: null }; Veams = VEAMS; if (!templates) { console.error("VeamsTemplater :: You need to pass an object which contains your templates (obj.templates)!"); return; } if (!engine) { console.error("VeamsTemplater :: You need to pass a handlebars instance by providing obj.engine!"); return; } this.options = { engine: engine, templates: templates, partials: partials, helpers: helpers }; this.initialize(); } Templater.prototype.initialize = function () { if (this.options.helpers) { this.registerHelpers(); } return this.addTemplater(); }; Templater.prototype.registerHelpers = function () { if (!Array.isArray(this.options.helpers)) { console.error("VeamsTemplater :: You need to pass the helpers as an array!"); return; } for (var i = 0; i < this.options.helpers.length; i++) { var helper = this.options.helpers[i]; if (helper.register) { this.options.engine.registerHelper(helper.register(this.options.engine)); } else { console.error("VeamsTemplater :: Your helper does not have a register function, see: " + helper); } } }; Templater.prototype.addTemplater = function () { if (Veams.templater) { console.warn('It seems that you are already using Veams.templater! Veams is overriding it now!'); } if (this.options.partials) { this.options.engine.partials = this.options.partials(this.options.engine); } Veams.templater = { engine: this.options.engine, templates: this.options.templates(this.options.engine), partials: this.options.partials ? this.options.partials(this.options.engine) : {}, helpers: this.options.helpers, render: function (tplName, data) { if (data === void 0) { data = {}; } if (!Veams.templater.templates[tplName]) { console.error("VeamsTemplater :: Template " + tplName + " not found."); return; } return Veams.templater.templates[tplName](data); } }; return Veams; }; return Templater; }()); exports.Templater = Templater; /** * Represents a templater plugin which you can use to render your precompiled handlebars templates. * You can also register custom helpers by providing them in an array! * * @module VeamsTemplater * * @author Sebastian Fitzner */ var TemplaterPlugin = { options: { engine: function () { }, templates: function () { }, partials: function () { }, helpers: [] }, pluginName: 'Templater', initialize: function (Veams, _a) { var engine = _a.engine, templates = _a.templates, partials = _a.partials, helpers = _a.helpers; return new Templater(Veams, { engine: engine, templates: templates, partials: partials, helpers: helpers }); } }; exports.default = TemplaterPlugin; //# sourceMappingURL=index.js.map