[IMP] qweb: add option to allow duplicate templates

This commit is contained in:
Géry Debongnie
2019-06-24 22:41:02 +02:00
parent 34695883c2
commit 4a889b7b6b
3 changed files with 16 additions and 6 deletions
+3 -1
View File
@@ -95,12 +95,14 @@ It's API is quite simple:
const qweb = new owl.QWeb(TEMPLATES); const qweb = new owl.QWeb(TEMPLATES);
``` ```
- **`addTemplate(name, xmlStr)`**: add a specific template. - **`addTemplate(name, xmlStr, allowDuplicate)`**: add a specific template.
```js ```js
qweb.addTemplate("mytemplate", "<div>hello</div>"); qweb.addTemplate("mytemplate", "<div>hello</div>");
``` ```
If the optional `allowDuplicate` is set to `true`, then `QWeb` will simply return whenever a template is added for a second time. Otherwise, `QWeb` will crash.
- **`addTemplates(xmlStr)`**: add a list of templates (identified by `t-name` - **`addTemplates(xmlStr)`**: add a list of templates (identified by `t-name`
attribute). attribute).
+4 -1
View File
@@ -173,7 +173,10 @@ export class QWeb extends EventBus {
* Add a template to the internal template map. Note that it is not * Add a template to the internal template map. Note that it is not
* immediately compiled. * immediately compiled.
*/ */
addTemplate(name: string, xmlString: string) { addTemplate(name: string, xmlString: string, allowDuplicate?: boolean) {
if (allowDuplicate && name in this.templates) {
return;
}
const doc = parseXML(xmlString); const doc = parseXML(xmlString);
if (!doc.firstChild) { if (!doc.firstChild) {
throw new Error("Invalid template (should not be empty)"); throw new Error("Invalid template (should not be empty)");
+9 -4
View File
@@ -25,13 +25,15 @@ describe("static templates", () => {
}); });
test("simple dynamic value", () => { test("simple dynamic value", () => {
qweb.addTemplate("test", "<t><t t-esc=\"text\"/></t>"); qweb.addTemplate("test", '<t><t t-esc="text"/></t>');
expect(renderToString(qweb, "test", {text: "hello vdom"})).toBe("hello vdom"); expect(renderToString(qweb, "test", { text: "hello vdom" })).toBe(
"hello vdom"
);
}); });
test("simple string, with some dynamic value", () => { test("simple string, with some dynamic value", () => {
qweb.addTemplate("test", "<t>hello <t t-esc=\"text\"/></t>"); qweb.addTemplate("test", '<t>hello <t t-esc="text"/></t>');
expect(renderToString(qweb, "test", {text: "vdom"})).toBe("hello vdom"); expect(renderToString(qweb, "test", { text: "vdom" })).toBe("hello vdom");
}); });
test("empty div", () => { test("empty div", () => {
@@ -71,6 +73,9 @@ describe("error handling", () => {
test("cannot add twice the same template", () => { test("cannot add twice the same template", () => {
qweb.addTemplate("test", `<t></t>`); qweb.addTemplate("test", `<t></t>`);
expect(() => qweb.addTemplate("test", "<div/>", true)).not.toThrow(
"already defined"
);
expect(() => qweb.addTemplate("test", "<div/>")).toThrow("already defined"); expect(() => qweb.addTemplate("test", "<div/>")).toThrow("already defined");
}); });