node js ejs code example

Example 1: ejs if else

<h1>Hey <%= title.toUpperCase() %>, Welcome </h1>

<% if(title.toLowerCase() === "Rabbit"){ %>
<p>Welcome Code_Breaker!</p>
<% } else { %>
<p>You are not Code_Breaker</p>
<% } %>

Example 2: how to include in ejs

<%- include('./partials/nav.ejs') %>

Example 3: express view engine', 'ejs

app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));

Example 4: ejs

npm install ejs	//install ejs in cmd

Example 5: how to use ejs with client side ejs

<div id="output"></div>
<script src="ejs.min.js"></script>
<script>
  let people = ['geddy', 'neil', 'alex'],
      html = ejs.render('<%= people.join(", "); %>', {people: people});
  // With jQuery:
  $('#output').html(html);
  // Vanilla JS:
  document.getElementById('output').innerHTML = html;
</script>

Example 6: ejs tutorial

<ul>
  <% users.forEach(function(user){ %>
    <%- include('user/show', {user: user}); %>
  <% }); %>
</ul>