Skip to content

EJS View Engine

GamanJS also supports EJS (Embedded JavaScript Templates) as one of its view engines for server-side rendering. EJS is known for its simplicity and compatibility with regular HTML.

you need to install the following packages

Terminal window
npm install @gaman/ejs

Integrate the EJS view engine in your index.ts:

defineBootstrap((app) => {
app.registerIntegration(
ejs({
cache: false,
})
);
});

By default, views are loaded from src/views. To customize the view root directory:

ejs({
viewPath: "src/custom-views",
});

  • Directorysrc
    • Directoryviews
      • index.njk
<!DOCTYPE html>
<html lang="en">
<head>
<title><%= title %></title>
</head>
<body>
<h1>Welcome to GamanJS with EJS!</h1>
</body>
</html>

Inside a Routes, render views using r.render():

export default defineRoutes(() => ({
"/": () => {
return r.render("index", {
title: "GamanJS | Web Application Framework",
});
},
}));

This will render the src/views/index.ejs file with the provided title variable.


  • Views are located in src/views by default.
  • Render templates using Res.render(viewName, data).
  • Customize the view path with the viewPath option.

Other view engines such as Pug, Handlebars, etc. may be supported in the future.


For more on EJS templating features like includes, partials, and conditionals, visit the EJS documentation.