EJS View Engine
Overview
Section titled “Overview”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.
Installation
Section titled “Installation”you need to install the following packages
npm install @gaman/ejs
Integration
Section titled “Integration”Integrate the EJS view engine in your index.ts
:
defineBootstrap((app) => { app.registerIntegration( ejs({ cache: false, }) );});
Custom View Directory
Section titled “Custom View Directory”By default, views are loaded from src/views
. To customize the view root directory:
ejs({ viewPath: "src/custom-views",});
Directory Structure
Section titled “Directory Structure”Directorysrc
Directoryviews
- index.njk
Example: index.ejs
Section titled “Example: index.ejs”<!DOCTYPE html><html lang="en"> <head> <title><%= title %></title> </head> <body> <h1>Welcome to GamanJS with EJS!</h1> </body></html>
Rendering a View
Section titled “Rendering a View”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.
Summary
Section titled “Summary”- 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.