CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ejs

Embedded JavaScript templates for generating HTML markup with plain JavaScript.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

EJS (Embedded JavaScript Templates)

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.

Package Information

  • Package Name: ejs
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install ejs

Core Imports

const ejs = require("ejs");

For modern ES modules:

import * as ejs from "ejs";

Individual imports:

const { render, renderFile, compile } = require("ejs");

Basic Usage

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" });

Architecture

EJS is built around several key components:

  • Template Engine: Core parsing and compilation system that converts EJS templates into JavaScript functions
  • Delimiters System: Configurable tag system (<% %>, <%= %>, <%- %>) for different types of JavaScript embedding
  • Include System: Template composition through file includes with path resolution
  • Caching Layer: Template function caching for improved performance
  • Express Integration: Native support for Express.js view engine pattern
  • Client/Server Rendering: Dual-mode operation for both Node.js and browser environments

Template Syntax Overview

EJS 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 variants

Capabilities

Template Compilation

Core 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);

Template Compilation

Template Rendering

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);

Template Rendering

Template Class

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();
}

Template Class

Configuration and Utilities

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);

Configuration

Command Line Interface

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

Command Line Interface

Types

// 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'

Install with Tessl CLI

npx tessl i tessl/npm-ejs
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/ejs@3.1.x
Publish Source
CLI
Badge
tessl/npm-ejs badge