Skip to content

Commit

Permalink
feat(@artusx/plugin-ejs): support pass views options
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed Apr 30, 2024
1 parent a5fce1a commit 39bb407
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/apps/artusx-koa/src/config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default () => {

const ejs: EjsConfig = {
root: path.resolve(__dirname, '../view-ejs'),
views: [path.resolve(__dirname, '../view-ejs/user')],
layout: {
'layout extractScripts': true,
'layout extractStyles': true,
Expand Down
8 changes: 8 additions & 0 deletions packages/apps/artusx-koa/src/controller/ejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ export default class EjsController {
layout: false,
});
}

@GET('/user-show')
async userShow(ctx: ArtusXContext) {
ctx.body = await this.ejs.render('user/show.ejs', {
name: 'hello world',
layout: false,
});
}
}
6 changes: 5 additions & 1 deletion packages/apps/artusx-koa/src/view-ejs/user/show.ejs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
<div>
<h2>show</h2>
<%= name %>
</div>

<%= name %>
<%- include('sub/info') %>
4 changes: 4 additions & 0 deletions packages/apps/artusx-koa/src/view-ejs/user/sub/info.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<h2>info</h2>
<%= name %>
</div>
6 changes: 4 additions & 2 deletions packages/plugins/ejs/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type EjsContext = Record<string, any>;

export type EjsConfig = {
root: string | string[];
views: string[];
layout?: Record<string, any>;
options?: Options;
};
Expand All @@ -87,9 +88,9 @@ export default class EjsClient {
return;
}

const { root, layout, options } = config;
const { root, views: defaultViews = [], layout, options } = config;

let views: string[] = [];
let views: string[] = defaultViews;

// root dirs
if (typeof root === 'string') {
Expand Down Expand Up @@ -131,6 +132,7 @@ export default class EjsClient {
this.layoutConfig = layout;
this.viewOptions = {
...options,
root,
views: Array.from(new Set(views)),
};
}
Expand Down

0 comments on commit 39bb407

Please sign in to comment.