Skip to content

EJS

@gaman/ejs is the official middleware for integrating EJS (Embedded JavaScript Templates) as a view engine in the GamanJS framework.

With this, you can render .ejs template files (or other extensions) directly from the controller/handler using the built-in view system of GamanJS.

Terminal window
npm install @gaman/ejs ejs

Add the EJS middleware to your application:

index.ts
import { ejs } from "@gaman/ejs";
defineBootstrap((app) => {
app.mount(
ejs({
viewPath: "src/views", // template folder
})
);
});

Create the file src/views/index.ejs

<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1>Hello, <%= name %>!</h1>
</body>
</html>

In the route handler, use Res.render():

AppRoutes.ts
route.get("/", (ctx) => {
return Res.render("index", {
title: "My First EJS Page",
name: "GamanJS 🚀"
});
});

| Name | Type | Default | Description |

|-------------|-------------------------------------------------------------|---------------|-----------|

| viewPath | string | "src/views" | Root directory where EJS templates are located. |


Please read the more detailed documentation about EJS (Embedded JavaScript Templates)