diff --git a/src/qweb.ts b/src/qweb.ts index 2c73a072..dd6ac2c3 100644 --- a/src/qweb.ts +++ b/src/qweb.ts @@ -182,7 +182,7 @@ export class QWeb { templates: { [name: string]: CompiledTemplate } = {}; directives: Directive[] = []; - constructor() { + constructor(data?: string) { [ forEachDirective, escDirective, @@ -196,6 +196,10 @@ export class QWeb { refDirective, widgetDirective ].forEach(d => this.addDirective(d)); + + if (data) { + this.loadTemplates(data); + } } utils = { diff --git a/tests/__snapshots__/qweb.test.ts.snap b/tests/__snapshots__/qweb.test.ts.snap index 32f1fab4..1ae113c6 100644 --- a/tests/__snapshots__/qweb.test.ts.snap +++ b/tests/__snapshots__/qweb.test.ts.snap @@ -464,6 +464,17 @@ exports[`foreach iterate, position 1`] = ` }" `; +exports[`loading templates can initialize qweb with a string 1`] = ` +"function anonymous(context,extra +) { + let h = this.utils.h; + let c1 = [], p1 = {key:1}; + let vn1 = h('div', p1, c1); + c1.push({text: \`jupiler\`}); + return vn1; +}" +`; + exports[`loading templates can load a few templates from a xml string 1`] = ` "function anonymous(context,extra ) { diff --git a/tests/qweb.test.ts b/tests/qweb.test.ts index 2fda7ca9..abd58407 100644 --- a/tests/qweb.test.ts +++ b/tests/qweb.test.ts @@ -878,6 +878,16 @@ describe("t-ref", () => { }); describe("loading templates", () => { + test("can initialize qweb with a string", () => { + const data = ` + + +
jupiler
+
`; + const qweb = new QWeb(data); + expect(renderToString(qweb, "hey")).toBe("
jupiler
"); + }); + test("can load a few templates from a xml string", () => { const data = `