Skip to content

Commit

Permalink
[IMP] template set config: getTemplate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Polymorphe57 committed Jan 10, 2024
1 parent 70101e4 commit d654332
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/runtime/template_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface TemplateSetConfig {
translatableAttributes?: string[];
translateFn?: (s: string) => string;
templates?: string | Document | Record<string, string>;
getTemplate?: (s: string) => (Element|string|null);
}

export class TemplateSet {
Expand All @@ -22,6 +23,7 @@ export class TemplateSet {
dev: boolean;
rawTemplates: typeof globalTemplates = Object.create(globalTemplates);
templates: { [name: string]: Template } = {};
getRawTemplate?: (s: string) => (Element|string|null);
translateFn?: (s: string) => string;
translatableAttributes?: string[];
Portal = Portal;
Expand All @@ -39,6 +41,7 @@ export class TemplateSet {
}
}
}
this.getRawTemplate = config.getTemplate;
}

addTemplate(name: string, template: string | Element) {
Expand Down Expand Up @@ -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 {
Expand Down
52 changes: 52 additions & 0 deletions tests/compiler/__snapshots__/template_set.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,55 @@ exports[`loading templates can load a few templates from an XMLDocument 2`] = `
}
}"
`;
exports[`loading templates function getTemplate in config 1`] = `
"function anonymous(app, bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
let block1 = createBlock(\`<div>Hello World!</div>\`);
return function template(ctx, node, key = \\"\\") {
return block1();
}
}"
`;
exports[`loading templates function getTemplate in config 2`] = `
"function anonymous(app, bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
let block1 = createBlock(\`<div>Hello World!</div>\`);
return function template(ctx, node, key = \\"\\") {
return block1();
}
}"
`;
exports[`loading templates function getTemplate in config 3`] = `
"function anonymous(app, bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
let block1 = createBlock(\`<div>Hello World!</div>\`);
return function template(ctx, node, key = \\"\\") {
return block1();
}
}"
`;
exports[`loading templates function getTemplate in config 4`] = `
"function anonymous(app, bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
let block1 = createBlock(\`<div>Hello World!</div>\`);
return function template(ctx, node, key = \\"\\") {
return block1();
}
}"
`;
60 changes: 60 additions & 0 deletions tests/compiler/template_set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,64 @@ describe("loading templates", () => {
context.addTemplates(xml);
expect(Object.keys(context.rawTemplates)).toEqual([]);
});

test("function getTemplate in config", () => {
const context = new TestContext({
getTemplate: (name) => {
if (name === "main") {
const data = `<div>Hello World!</div>`;
const xml = new DOMParser().parseFromString(data, "text/xml");
return xml.firstChild as Element;
}
return null;
}
});
const result = context.renderToString("main");
expect(result).toBe("<div>Hello World!</div>");
});

test("function getTemplate in config", () => {
const context = new TestContext({
getTemplate: (name) => {
if (name === "main") {
const doc = new Document();
const div = doc.createElement("div") as Element;
div.append(doc.createTextNode("Hello World!"));
return div;
}
return null;
}
});
const result = context.renderToString("main");
expect(result).toBe("<div>Hello World!</div>");
});

test("function getTemplate in config", () => {
const context = new TestContext({
getTemplate: (name) => {
if (name === "main") {
return `<div>Hello World!</div>`
}
return null;
}
});
const result = context.renderToString("main");
expect(result).toBe("<div>Hello World!</div>");
});

test("function getTemplate in config", () => {
const context = new TestContext({
getTemplate: () => {
return null;
}
});
const data = `<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
<div t-name="main">Hello World!</div>
</templates>`;
const xml = new DOMParser().parseFromString(data, "text/xml");
context.addTemplates(xml);
const result = context.renderToString("main");
expect(result).toBe("<div>Hello World!</div>");
});
});

0 comments on commit d654332

Please sign in to comment.