Embedded JavaScript (EJS) is a simple templating language that lets you generate HTML with plain JavaScript. It allows developers to embed JavaScript logic directly in their HTML files, rendering data dynamically on the server side. With EJS, you can easily loop over arrays, conditionally render content, and inject variables into your templates.
- Simple Syntax: EJS uses plain JavaScript to dynamically generate HTML content.
- Server-Side Rendering: Renders dynamic web pages on the server before sending them to the client.
- Template Reuse: Supports partials and template reuse to maintain consistency across your app.
- Fast and Lightweight: EJS is designed to be minimal and efficient for rendering views.
- JavaScript Logic: Includes native JavaScript syntax for loops, conditions, and more.
EJS uses the following syntax to embed JavaScript into HTML:
<%= variable %>: Outputs the value of variable and escapes HTML characters. <%- variable %>: Outputs the value of variable without escaping HTML characters. <% code %>: Runs JavaScript code but does not output anything (used for logic like loops and conditions).
Outputs a value and escapes HTML characters to prevent XSS attacks.
Outputs a value without escaping HTML characters, allowing raw HTML to be rendered.
Executes JavaScript logic (like loops and conditionals) without rendering any output.
Embeds another EJS template within the current template, allowing for reusable components.
Adds comments in the template that are not rendered in the output.
Executes a block of code a specified number of times.
Executes a block of code if the specified condition is true.