[IMP] qweb: add registerTemplate method

This commit is contained in:
Géry Debongnie
2019-09-12 10:38:53 +02:00
parent 5bf7a5c671
commit d57017b6ed
4 changed files with 47 additions and 2 deletions
+18 -1
View File
@@ -129,7 +129,7 @@ function parseXML(xml: string): Document {
// QWeb rendering engine
//------------------------------------------------------------------------------
export class QWeb extends EventBus {
templates: { [name: string]: Template } = {};
templates: { [name: string]: Template };
static utils = UTILS;
static components = Object.create(null);
@@ -141,6 +141,8 @@ export class QWeb extends EventBus {
};
static DIRECTIVES: Directive[] = [];
static TEMPLATES: { [name: string]: Template } = {};
h = h;
// dev mode enables better error messages or more costly validations
static dev: boolean = false;
@@ -160,6 +162,7 @@ export class QWeb extends EventBus {
constructor(data?: string) {
super();
this.templates = Object.create(QWeb.TEMPLATES);
if (data) {
this.addTemplates(data);
}
@@ -184,6 +187,20 @@ export class QWeb extends EventBus {
QWeb.components[name] = Component;
}
/**
* Register globally a template. All QWeb instances will obtain their
* templates from their own template map, and then, from the global static
* TEMPLATES property.
*/
static registerTemplate(name: string, template: string) {
if (QWeb.TEMPLATES[name]) {
throw new Error(`Template '${name}' has already been registered`);
}
const qweb = new QWeb();
qweb.addTemplate(name, template);
QWeb.TEMPLATES[name] = qweb.templates[name];
}
/**
* Add a template to the internal template map. Note that it is not
* immediately compiled.