Skip to content

Commit

Permalink
view add ejs
Browse files Browse the repository at this point in the history
  • Loading branch information
kaivean committed Oct 29, 2023
1 parent 76e014e commit 306f6ad
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/view/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import LRU from 'lru-cache';
type PartialRecord<K extends keyof any, T> = {
[P in K]?: T;
};
const supportedEngines = ['swig', 'nunjucks'] as const;
const supportedEngines = ['swig', 'nunjucks', 'ejs'] as const;
type supportedEnginesType = typeof supportedEngines[number];;
type EngineList = PartialRecord<supportedEnginesType, any>;

Expand Down Expand Up @@ -84,6 +84,7 @@ async function plugin(fastify: FastifyInstance, opts: HothViewOptions) {
const renders = {
swig: viewSwig,
nunjucks: viewNunjucks,
ejs: viewEjs,
};

let swig: Swig;
Expand Down Expand Up @@ -177,6 +178,21 @@ async function plugin(fastify: FastifyInstance, opts: HothViewOptions) {
swig.renderFile(join(templatesDir!, getPage(page)), data, done);
}

function viewEjs(
this: FastifyReply,
page: string,
data: Record<string, unknown>,
done: (err: Error, html: string) => void
) {
if (!page) {
this.send(new Error('Missing page'));
return;
}

data = Object.assign({}, defaultCtx, data);
engine.renderFile(join(templatesDir!, getPage(page)), data, done);
}

function viewNunjucks(
this: FastifyReply,
page: string,
Expand Down

0 comments on commit 306f6ad

Please sign in to comment.