Short for Embedded JavaScript, it allows the addition of Javascript syntax into HTML markup.
$ npm i ejs
In app.js
, for ease development, the app can set the defaul view engine to be the .ejs format.
app.set("view engine", "ejs");
res.render("results", { data }); // there is no need to add .ejs to the "results" string
Create a file with the .ejs
extension in order to create the template. HMTlL markup can be used, along with Javascript syntax, adding some <%= %>
tags, if there is something to return:
<h1>You fell in love with <%= thing.toUpperCase() %></h1>
If nothing is returned, then <% %>
is used:
<% arr.forEach((item) => { %>
<li> item </li>
<% >}) %>
There is the possibility to also add other files inside another, with the inclusion of partials:
<%- include('partials/header') %>