diff --git a/src/config.ts b/src/config.ts index dbb180d..1c49d16 100644 --- a/src/config.ts +++ b/src/config.ts @@ -69,6 +69,9 @@ export interface EtaConfig { /** Directory that contains templates */ views?: string; + + /** Control template file extension defaults. Default `.eta` */ + defaultExtension?: string; } /* END TYPES */ @@ -95,6 +98,7 @@ const defaultConfig: EtaConfig = { tags: ["<%", "%>"], useWith: false, varName: "it", + defaultExtension: ".eta", }; export { defaultConfig }; diff --git a/src/file-handling.ts b/src/file-handling.ts index 816aa36..4508bee 100644 --- a/src/file-handling.ts +++ b/src/file-handling.ts @@ -40,6 +40,7 @@ export function resolvePath( } const baseFilePath = options && options.filepath; + const defaultExtension = this.config.defaultExtension === undefined ? ".eta" : this.config.defaultExtension; // how we index cached template paths const cacheIndex = JSON.stringify({ @@ -48,7 +49,7 @@ export function resolvePath( views: this.config.views, }); - templatePath += path.extname(templatePath) ? "" : ".eta"; + templatePath += path.extname(templatePath) ? "" : defaultExtension; // if the file was included from another template if (baseFilePath) { diff --git a/test/file-handling.spec.ts b/test/file-handling.spec.ts index 9421c44..66ae623 100644 --- a/test/file-handling.spec.ts +++ b/test/file-handling.spec.ts @@ -68,3 +68,21 @@ describe("file handling errors", () => { }).toThrow(); }); }); + +describe("filepath default extension tests", () => { + console.log("Templates are in ", viewsDir) + + it("works with defaultExtension", async () => { + const eta = new Eta({ views: viewsDir, cache: true, defaultExtension: ".tmpl" }); + const templateResult = await eta.render("template-extn", { name: "TMPL Run" }); + + expect(templateResult).toEqual(`Hi TMPL Run`); + }); + + it("works with no extension", async () => { + const eta = new Eta({ views: viewsDir, cache: true, defaultExtension: "" }); + const templateResult = await eta.render("template-without-extn", { name: "TMPL Run" }); + + expect(templateResult).toEqual(`Hi TMPL Run`); + }); +}) diff --git a/test/templates/template-extn.tmpl b/test/templates/template-extn.tmpl new file mode 100644 index 0000000..3d352ec --- /dev/null +++ b/test/templates/template-extn.tmpl @@ -0,0 +1 @@ +Hi <%= it.name %> \ No newline at end of file diff --git a/test/templates/template-without-extn b/test/templates/template-without-extn new file mode 100644 index 0000000..3d352ec --- /dev/null +++ b/test/templates/template-without-extn @@ -0,0 +1 @@ +Hi <%= it.name %> \ No newline at end of file