EJS Cheatsheet - EJS Template Engine Reference

This reference is for Node and Express developers who render pages serverside with EJS, the template engine you configure in one line and use everywhere. It covers the tag syntax (<% code %>, <%= escaped output %>, <%- raw HTML %>, <%- include %>, comments), how to branch and loop over the locals you pass in, splitting pages into includes, sharing a layout, and calling res.render from Express. Unlike a generic EJS primer, entries are grouped by what each part of the template does, and note when output is escaped vs raw. After reading you should be able to build an Express view, loop over data with <%- for %>, escape user content with <%=, and reuse header/footer via includes.

Languages·35 commands·Last updated 2026-07-21
ejsTemplatesnodejs

Basic Syntax 5

<% code %>
Execute JS code (no output)
<%= value %>
Output variable (HTML-escaped)
<%- value %>
Output variable (raw HTML, no escape)
<%# comment %>
Comment (not rendered)
<%_ code %>
Execute code, strip surrounding whitespace

Conditionals 3

<% if (condition) { %>...<% } %>
if condition
<% if (condition) { %>...<% } else { %>...<% } %>
if-else condition
<% if (a) { %>...<% } else if (b) { %>...<% } else { %>...<% } %>
multi-branch if-else if

Loops 4

<% for (let i = 0; i < items.length; i++) { %>...<% } %>
for loop
<% items.forEach(item => { %>...<% }) %>
forEach loop
<% for (const item of items) { %>...<% } %>
for...of loop
<% for (const key in obj) { %>...<% } %>
for...in loop

Includes 4

<%- include("header") %>
Include header.ejs
<%- include("footer") %>
Include footer.ejs
<%- include("sidebar", { user }) %>
Include with parameters
<%- include("partials/nav") %>
Include template in a subfolder

Layout & Inheritance 3

<%- body %>
Render child template in layout
<% layout("layout") -%>
Set layout file (needs express-ejs-layouts)
<%- include("header", {title: "Home"}) %>
Pass params to an included template

Common Tricks 5

<%= user?.name || "Guest" %>
Optional chaining with default
<%= JSON.stringify(data) %>
Output a JSON string
<%= new Date().toLocaleDateString() %>
Format a date
<%= items.join(", ") %>
Join array into a string
<%= Math.round(price * 100) / 100 %>
Round to 2 decimals

Express Integration 4

app.set("view engine", "ejs")
Set EJS as the view engine
res.render("index", { title: "Home" })
Render a template with data
app.set("views", "./views")
Set the views directory
res.render("users/list", { users })
Render a template in a subfolder

Render API & Config 7

ejs.render(str, data)
Render a template string to HTML
ejs.renderFile("tpl.ejs", data, (err, html) => {})
Render a file asynchronously
ejs.compile(str, { client: true })
Compile to a client-side function
ejs.render(str, data, { cache: true })
Enable cached rendering (production)
ejs.delimiter = "$"
Set custom delimiter to $
<%= locals.user %>
Access a variable from locals
const fn = ejs.compile(str); fn(data)
Compile once, reuse the render fn

Tips

  • <%= %> auto-escapes HTML to prevent XSS; <%- %> outputs raw HTML without escaping.
  • include loads synchronously, inserting the included template at the current position.
  • EJS supports full JavaScript syntax inside <% %>.
  • Layout inheritance needs the express-ejs-layouts middleware, or include header/footer manually.
  • Cache templates for performance: app.set('view cache', true).
  • Disable cache in dev: app.set('view cache', false) so edits apply immediately.
  • Functions from ejs.compile run in the browser, fitting client-side rendering.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us