EJS is a powerful embedded JavaScript templating engine that enables developers to generate HTML markup with plain JavaScript. It provides a simple and intuitive syntax for embedding JavaScript code directly into templates using special tags, supports includes for template composition, offers configurable delimiters, provides both client-side and server-side rendering capabilities, includes static caching for performance optimization, and integrates seamlessly with Express.js and other web frameworks.
npm install ejsconst ejs = require("ejs");For modern ES modules:
import * as ejs from "ejs";Individual imports:
const { render, renderFile, compile } = require("ejs");const ejs = require("ejs");
// Simple template rendering
const template = `<h1><%= title %></h1><p><%= message %></p>`;
const data = { title: "Hello", message: "Welcome to EJS!" };
const html = ejs.render(template, data);
console.log(html); // <h1>Hello</h1><p>Welcome to EJS!</p>
// Render template from file
ejs.renderFile("./template.ejs", data, (err, str) => {
if (err) throw err;
console.log(str);
});
// Compile template for reuse
const compiledTemplate = ejs.compile(template);
const output1 = compiledTemplate(data);
const output2 = compiledTemplate({ title: "Hi", message: "Another message" });EJS is built around several key components:
<% %>, <%= %>, <%- %>) for different types of JavaScript embeddingEJS uses several types of tags for different operations:
<% %>: Control flow (if statements, loops) - no output<%= %>: Escaped output - HTML entities are escaped for security<%- %>: Unescaped raw output - direct HTML insertion<%# %>: Comments - not included in output<%% %>: Literal <% - outputs the tag itself<% _%> / <%_ %> / _%>: Whitespace slurping variantsCore template compilation functionality for converting EJS template strings into reusable JavaScript functions.
/**
* Compile the given template string into a template function
* @param template - EJS template string
* @param opts - Compilation options
* @returns Template function or client function
*/
function compile(template, opts);Direct template rendering functionality for processing templates with data and generating output.
/**
* Render the given template with data
* @param template - EJS template string
* @param data - Template data object
* @param opts - Compilation and rendering options
* @returns Rendered string or Promise<String>
*/
function render(template, data, opts);
/**
* Render an EJS file at the given path
* @param path - Path to the EJS file
* @param data - Template data object
* @param opts - Compilation and rendering options
* @param callback - Optional callback function
* @returns String/Promise or calls callback
*/
function renderFile(path, data, opts, callback);Advanced template handling through the Template class for fine-grained control over compilation process.
/**
* EJS template class for advanced template compilation
* @param text - Template text
* @param opts - Template options
*/
class Template {
constructor(text, opts);
compile();
generateSource();
parseTemplateText();
scanLine(line);
createRegex();
}Global configuration properties and utility functions for customizing EJS behavior.
// Configuration properties
const cache; // Template function cache
let fileLoader; // Custom file loader function
let localsName; // Name of locals object
let promiseImpl; // Promise implementation
// Utility functions
function clearCache();
function resolveInclude(name, filename, isDir);
function escapeXML(markup);Complete command-line interface for rendering templates directly from the terminal with extensive configuration options.
# CLI command with options
ejs [options ...] template-file [data variables ...]
# Key options
-o, --output-file FILE # Write output to file
-f, --data-file FILE # JSON data input from file
-m, --delimiter CHAR # Custom template delimiter
-w, --rm-whitespace # Remove whitespace
-d, --debug # Debug mode// Options interface used across EJS functions
interface Options {
// Core options
delimiter?: string;
openDelimiter?: string;
closeDelimiter?: string;
filename?: string;
context?: object;
client?: boolean;
cache?: boolean;
debug?: boolean;
compileDebug?: boolean;
// Rendering options
strict?: boolean;
_with?: boolean;
rmWhitespace?: boolean;
async?: boolean;
localsName?: string;
outputFunctionName?: string;
escapeFunction?: Function;
destructuredLocals?: string[];
// Include/view options
views?: string[];
root?: string | string[];
includer?: Function;
legacyInclude?: boolean;
}
// Template parsing modes
const Template.modes = {
EVAL: 'eval',
ESCAPED: 'escaped',
RAW: 'raw',
COMMENT: 'comment',
LITERAL: 'literal'
};
// Version and metadata
const VERSION; // EJS version string
const name; // Package name 'ejs'