如何将EJS模板引擎与Nestjs一起使用?

发布于 2025-02-09 01:04:22 字数 166 浏览 0 评论 0 原文

我想将EJS用作Nestjs中的模板引擎。使用Express,我可以在主文件中配置EJ:

app.set("view engine", "ejs");

如何最好用Nestjs实现此功能? Nestjs不使用 .set 方法运送。

I would like to use EJS as my template engine in NestJS. With Express I can configure EJS in the main file like this:

app.set("view engine", "ejs");

How can I best implement this with NestJS? Nestjs does not ship with a .set method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2025-02-16 01:04:22

您可以在 app.setViewEngine('ejs') in main.ts 中配置该。首先,安装它:

npm i ejs

在下面的行中,您会告诉Express, public 目录将用于存储静态资产, views 将包含模板,并且 EJS 模板引擎应用于渲染HTML输出。

// main.ts
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(
    AppModule,
  );
  /*
     Here it's assumed that public and views are in the root directory,
     alongside src. You can put them wherever you want, 
     just use the correct path if you use another folder.
  */
  app.useStaticAssets(join(__dirname, '..', 'public'));
  app.setBaseViewsDir(join(__dirname, '..', 'views'));
  app.setViewEngine('ejs');

  await app.listen(3000);
}
bootstrap();

以下是您将如何在控制器中渲染模板。您正在渲染 index.ejs ,并将消息传递作为参数。

// app.controller.ts
import { Get, Controller, Render } from '@nestjs/common';

@Controller()
export class AppController {
  @Get()
  @Render('index')
  root() {
    return { message: 'Hello world!' };
  }
}

最后,您将使用该传递的消息变量 index.ejs 像这样:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>App</title>
  </head>
  <body>
    <%= message %>
  </body>
</html>

您可以在官方文档

You can configure that with the help of app.setViewEngine('ejs') in main.ts. First, install it:

npm i ejs

With the lines below, you would have told Express that the public directory will be used for storing static assets, views will contain templates, and the ejs template engine should be used to render HTML output.

// main.ts
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(
    AppModule,
  );
  /*
     Here it's assumed that public and views are in the root directory,
     alongside src. You can put them wherever you want, 
     just use the correct path if you use another folder.
  */
  app.useStaticAssets(join(__dirname, '..', 'public'));
  app.setBaseViewsDir(join(__dirname, '..', 'views'));
  app.setViewEngine('ejs');

  await app.listen(3000);
}
bootstrap();

And below is how you would render a template in a Controller. You are rendering index.ejs and passing message as a parameter.

// app.controller.ts
import { Get, Controller, Render } from '@nestjs/common';

@Controller()
export class AppController {
  @Get()
  @Render('index')
  root() {
    return { message: 'Hello world!' };
  }
}

Finally, you would use that passed message variable inside index.ejs like this:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>App</title>
  </head>
  <body>
    <%= message %>
  </body>
</html>

You can read more on the official documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文