Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for #265 - Enable support for default extensions. #266

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export interface EtaConfig {

/** Directory that contains templates */
views?: string;

/** Control template file extension defaults. Default `.eta` */
defaultExtension?: string;
}

/* END TYPES */
Expand All @@ -95,6 +98,7 @@ const defaultConfig: EtaConfig = {
tags: ["<%", "%>"],
useWith: false,
varName: "it",
defaultExtension: ".eta",
};

export { defaultConfig };
3 changes: 2 additions & 1 deletion src/file-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions test/file-handling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
})
1 change: 1 addition & 0 deletions test/templates/template-extn.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi <%= it.name %>
1 change: 1 addition & 0 deletions test/templates/template-without-extn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi <%= it.name %>
Loading