From 39bb4072c3fc875b8dda2404475d1e0b5f086ef7 Mon Sep 17 00:00:00 2001 From: Suyi Date: Tue, 30 Apr 2024 11:59:40 +0000 Subject: [PATCH] feat(@artusx/plugin-ejs): support pass views options --- packages/apps/artusx-koa/src/config/config.default.ts | 1 + packages/apps/artusx-koa/src/controller/ejs.ts | 8 ++++++++ packages/apps/artusx-koa/src/view-ejs/user/show.ejs | 6 +++++- packages/apps/artusx-koa/src/view-ejs/user/sub/info.ejs | 4 ++++ packages/plugins/ejs/src/client.ts | 6 ++++-- 5 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 packages/apps/artusx-koa/src/view-ejs/user/sub/info.ejs diff --git a/packages/apps/artusx-koa/src/config/config.default.ts b/packages/apps/artusx-koa/src/config/config.default.ts index 31ec4d8..8cc84ae 100644 --- a/packages/apps/artusx-koa/src/config/config.default.ts +++ b/packages/apps/artusx-koa/src/config/config.default.ts @@ -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, diff --git a/packages/apps/artusx-koa/src/controller/ejs.ts b/packages/apps/artusx-koa/src/controller/ejs.ts index cf06e73..c35932e 100644 --- a/packages/apps/artusx-koa/src/controller/ejs.ts +++ b/packages/apps/artusx-koa/src/controller/ejs.ts @@ -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, + }); + } } diff --git a/packages/apps/artusx-koa/src/view-ejs/user/show.ejs b/packages/apps/artusx-koa/src/view-ejs/user/show.ejs index 83218e8..eae92ac 100644 --- a/packages/apps/artusx-koa/src/view-ejs/user/show.ejs +++ b/packages/apps/artusx-koa/src/view-ejs/user/show.ejs @@ -1,2 +1,6 @@ +
+

show

+ <%= name %> +
-<%= name %> \ No newline at end of file +<%- include('sub/info') %> diff --git a/packages/apps/artusx-koa/src/view-ejs/user/sub/info.ejs b/packages/apps/artusx-koa/src/view-ejs/user/sub/info.ejs new file mode 100644 index 0000000..b4649a9 --- /dev/null +++ b/packages/apps/artusx-koa/src/view-ejs/user/sub/info.ejs @@ -0,0 +1,4 @@ +
+

info

+ <%= name %> +
diff --git a/packages/plugins/ejs/src/client.ts b/packages/plugins/ejs/src/client.ts index f853641..6fa5a52 100644 --- a/packages/plugins/ejs/src/client.ts +++ b/packages/plugins/ejs/src/client.ts @@ -67,6 +67,7 @@ export type EjsContext = Record; export type EjsConfig = { root: string | string[]; + views: string[]; layout?: Record; options?: Options; }; @@ -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') { @@ -131,6 +132,7 @@ export default class EjsClient { this.layoutConfig = layout; this.viewOptions = { ...options, + root, views: Array.from(new Set(views)), }; }