<% code %>
Browse / EJS (Embedded JavaScript) Cheatsheet
EJS (Embedded JavaScript) Cheatsheet
A quick reference guide covering essential EJS syntax, tags, control flow, includes, data handling, and best practices for generating dynamic HTML with plain JavaScript.
EJS Basics & Output
Core EJS Tags
|
Execute JavaScript code. No output. |
|
Output the value of |
|
Output the raw, unescaped value of |
|
EJS comment. Not rendered in the output HTML. |
|
Include a partial template (older syntax, |
|
Execute JavaScript code, stripping leading whitespace. |
|
Output raw value, stripping trailing whitespace. |
Escaped vs Raw Output
Use
Output:
|
Use
Output:
|
Be extremely cautious with user-supplied data in |
Control Flow & Includes
Conditional Statements
Use
|
Ensure curly braces |
You can spread the logic across multiple EJS tags.
|
Loops
Iterate over arrays using Using
|
Using a standard
|
Looping over object properties:
|
Remember to close your loop tags ( |
Includes
Include other EJS files (partials) using
|
The |
You can pass local variables to the included partial as a second argument (an object). In
|
Variables from the parent scope are also available in the partial unless locals are explicitly passed. |
Data Handling & Features
Accessing Data
Data passed to Given data
|
Nested object properties can be accessed using dot notation. Given data
|
Be mindful of potential |
Defining Variables
You can define variables within
|
Variables defined this way are scoped to the current template or include. |
Avoid complex logic within templates. Ideally, prepare data and variables in your server-side code before passing them to EJS. |
Calling Functions
You can call functions defined in the data object passed to EJS, or standard JavaScript functions. Given data
|
Standard JS functions like
|
Custom Delimiters
Default delimiters: |
|
Change delimiters via options: |
Pass Example with delimiter |
Change open/close via options: |
Pass Example with delimiters |
Tips & Best Practices
Error Handling
EJS errors (syntax errors, runtime JS errors) during rendering will typically throw exceptions in your server-side code. |
Ensure you have proper |
Use the |
Check for the existence of variables or properties before accessing them if they might be missing.
|
Performance Considerations
EJS compiles templates into JavaScript functions on the first render (per file). |
In production, templates are cached by default. Ensure caching is enabled for performance. |
Avoid complex, heavy computations within EJS templates. Perform data processing in your application logic before rendering. |
Keep templates relatively small and use includes ( |
General Best Practices
Separation of Concerns: Keep presentation logic (HTML structure, loops, conditionals based on simple flags) in EJS templates and business logic (data fetching, complex calculations, database interactions) in your application code. |
Pass Only Necessary Data: Don’t dump your entire database object into the template context. Pass only the specific data required for that template to render. |
Consistent Formatting: Use consistent indentation and spacing within EJS tags to improve readability. |
Comments: Use |
Using EJS with Node.js/Express
Install EJS: |
Set EJS as the view engine in Express:
|
Render a view from a route handler:
|
Access passed data in
|