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

feat: adjust render types to be generic #278

Merged
merged 1 commit into from
Mar 16, 2024
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
12 changes: 6 additions & 6 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ function handleCache(this: Eta, template: string, options: Partial<Options>): Te
}
}

export function render(
export function render<T extends object>(
this: Eta,
template: string | TemplateFunction, // template name or template function
data: object,
data: T,
meta?: { filepath: string }
): string {
let templateFn: TemplateFunction;
Expand All @@ -60,10 +60,10 @@ export function render(
return res;
}

export function renderAsync(
export function renderAsync<T extends object>(
this: Eta,
template: string | TemplateFunction, // template name or template function
data: object,
data: T,
meta?: { filepath: string }
): Promise<string> {
let templateFn: TemplateFunction;
Expand All @@ -85,13 +85,13 @@ export function renderAsync(
return Promise.resolve(res);
}

export function renderString(this: Eta, template: string, data: object): string {
export function renderString<T extends object>(this: Eta, template: string, data: T): string {
const templateFn = this.compile(template, { async: false });

return render.call(this, templateFn, data);
}

export function renderStringAsync(this: Eta, template: string, data: object): Promise<string> {
export function renderStringAsync<T extends object>(this: Eta, template: string, data: T): Promise<string> {
const templateFn = this.compile(template, { async: true });

return renderAsync.call(this, templateFn, data);
Expand Down
6 changes: 5 additions & 1 deletion test/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import path from "node:path";

import { Eta } from "../src/index";

interface SimpleEtaTemplate {
name: string;
}

describe("basic functionality", () => {
const eta = new Eta();

Expand Down Expand Up @@ -139,7 +143,7 @@ describe("file rendering", () => {
const eta = new Eta({ views: path.join(__dirname, "templates") });

it("renders template file properly", () => {
const res = eta.render("simple.eta", { name: "friend" });
const res = eta.render<SimpleEtaTemplate>("simple.eta", { name: "friend" });

expect(res).toEqual("Hi friend");
});
Expand Down
Loading