Here’s a cheat sheet for EJS (Embedded JavaScript), a templating engine for JavaScript:
Basic Syntax
Output Variable:
<%= variable %>
Scriptlet (No Output):
<% code %>
Comment:
<%# comment %>
Control Flow
If Statement:
<% if (condition) { %>
<!-- Code to execute if condition is true -->
<% } %>
Else Statement:
<% if (condition) { %>
<!-- Code to execute if condition is true -->
<% } else { %>
<!-- Code to execute if condition is false -->
<% } %>
For Loop:
<% for (let i = 0; i < array.length; i++) { %>
<!-- Code to execute in each iteration -->
<% } %>
Foreach Loop:
<% array.forEach(function(item) { %>
<!-- Code to execute for each item -->
<% }); %>
Template Structure
Include Partial:
<% include('partial.ejs'); %>
Escaping
Escape HTML:
Escaping:
Escape HTML
Layouts
Set Layout:
<% layout('layout.ejs') %>
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= title %></title>
</head>
<body>
<h1><%= title %></h1>
<ul>
<% for (let i = 0; i < items.length; i++) { %>
<li><%= items[i] %></li>
<% } %>
</ul>
<%# This is a comment %>
</body>
</html>
This is a overview of EJS syntax and usage. You can customize and extend it based on your project’s requirements. EJS documentation is a great resource for more in-depth information.