[IMP] template set config: getTemplate function

A template set can now obtain a template by calling the function
getTemplate (if any) received in the initial config.
The returned value can be an Element instance, a template string, a
function (i.e. a compiled template), or null. In the last case, owl will
look into the template set rawTemplates object.
This commit is contained in:
Mathieu Duckerts-Antoine
2024-01-10 16:49:32 +01:00
committed by Sam Degueldre
parent 70101e4c66
commit 7b454dae66
4 changed files with 116 additions and 1 deletions
+4 -1
View File
@@ -13,6 +13,7 @@ export interface TemplateSetConfig {
translatableAttributes?: string[];
translateFn?: (s: string) => string;
templates?: string | Document | Record<string, string>;
getTemplate?: (s: string) => Element | Function | string | void;
}
export class TemplateSet {
@@ -22,6 +23,7 @@ export class TemplateSet {
dev: boolean;
rawTemplates: typeof globalTemplates = Object.create(globalTemplates);
templates: { [name: string]: Template } = {};
getRawTemplate?: (s: string) => Element | Function | string | void;
translateFn?: (s: string) => string;
translatableAttributes?: string[];
Portal = Portal;
@@ -39,6 +41,7 @@ export class TemplateSet {
}
}
}
this.getRawTemplate = config.getTemplate;
}
addTemplate(name: string, template: string | Element) {
@@ -77,7 +80,7 @@ export class TemplateSet {
getTemplate(name: string): Template {
if (!(name in this.templates)) {
const rawTemplate = this.rawTemplates[name];
const rawTemplate = this.getRawTemplate?.(name) || this.rawTemplates[name];
if (rawTemplate === undefined) {
let extraInfo = "";
try {