Control & Utility Tags
Used for Logic or Structure. These tags are used not to display data, but to control the flow of logic, structure your templates, or manage whitespace/comments. These are essential for adding loops, conditions, comments, and clean formatting in templates.
1. <% %> - Scriptlet Tag (Control-Flow, No Output)
Executes JavaScript for control flow (e.g., loops, conditionals, variable assignments) without directly outputting to the HTML.
example:
<% if (user.isAdmin) { %>
<p> Welcome, Admin! </p>
<% } %>
2. <%_ %> - Whitespace Slurping Scriptlet Tag
Removes all whitespace (spaces, newlines) before the tag, useful for clean HTML output.
example:
<ul<
<%_ notes.forEach(note => { %>
<li><%= note.title %></li>
<%_ }) %>
</ul>
//output for html like:
<ul>
<li>JavaScript Async/Await</li>
</ul>
//without it may like:
<ul>
<li>JavaScript Async/Await</li>
</ul>
3. <%# %> - Comment Tag
Adds comments in EJS that are not included in the rendered HTML and are not executed.
example:
<%# This is a comment, not visible in the HTML %>
<p>Visible content</p>
//output for html like:
<p>Visible content</p>
4. <%% %> - Outputs a Literal
Escapes the <% delimiter, rendering it as literal text in the output. If you want to print a literal <% in the HTML (for educational content or documentation), use <%%.
example:
<%%= "example" %>
//output:
<%= "example" %>
5. %> - Plain Ending Tag
Closes any EJS tag (<%, <%_, <%=, <%-, etc.).
6. -%> - Trim-Mode (Newline Slurp) Tag
Removes the newline character after the tag, reducing extra line breaks in the output, helping keep HTML compact.
example:
<ul<
<%_ notes.forEach(note => { %>
<li><%= note.title %></li>
<%_ }) %>
</ul>
//output for html like:
<ul>
<li>JavaScript Async/Await</li>
</ul>
//without it may like:
<ul>
<li>JavaScript Async/Await</li>
</ul>
7. _%> - Whitespace Slurping Ending Tag
Like -%>, but removes all whitespace after the tag, not just the newline. Useful for super clean output.
example:
<ul<
<%_ notes.forEach(note => { %>
<li><%= note.title %></li>
<%_ }) %>
</ul>
//output for html like:
<ul>
<li>JavaScript</li><li>Python</li>
</ul>