- Introduction ejs
- Passing data
- if else
- loop
- layout
- styling
- deploy on heroku
-
ejs stands for embedded javascript
-
ejs is a templating language.
-
templating language helps us to manipulate dynamic content in HTML document.
-
Templating language: ejs, handlebars, pug etc.
-
Ejs allows us to run plain js in HTML
-
Ejs simple, light weight, fast
-
Most downloaded templating language on npm
-
Founded in Feb, 2011
-
how to use ejs
// first install ejs: npm install ejs // inside the server app.set('view engine', 'ejs'); // create index.ejs inside views folder res.render('index',{});
-
create and pass the data
-
syntax:
<%= variable %>
let pLanguages = ["c", "c++"]; app.get("/", (req, res) => { res.render("index", { plNames: pLanguages }); }); //inside the index.ejs <li><%= plNames[0] %></li>
-
if, else syntax:
<% if () { %> // do whatever <% } else { %> // do whatever <% } %>
<% if(plNames){ %> <li><%= plNames[x] %></li> <% }else{ %> <p>no data found</p> <% } %>
-
loop control statement
-
syntax:
<% for () { %> // do whatever <% } %>
<% for(let x=0; x<plNames.length; x++){ %> <li><%= plNames[x] %></li> <% } %></plNames.length; >
- create layout and use it anywhere
- To use a layout syntax : <%- include("layout/header") %>
- project link on github: https://github.com/anisul-Islam/ejs-youtube-project