EJS

EJS is a simple templating language that lets you generate HTML markup with plain JavaScript. No religiousness about how to organize things. No reinvention of iteration and control-flow. It’s just plain JavaScript.

Features

  • Fast compilation and rendering
  • Simple template tags: <% %>
  • Custom delimiters (e.g., use <? ?> instead of <% %>)
  • Includes
  • Both server JS and browser support
  • Static caching of intermediate JavaScript
  • Static caching of templates
  • Complies with the Express view system

Install

It’s easy to install EJS with NPM.

$ npm install ejs

Use

Pass EJS a template string and some data. BOOM, you’ve got some HTML.

let ejs = require(‘ejs’),

people = [‘geddy’, ‘neil’, ‘alex’],

html = ejs.render(‘<%= people.join(“, “); %>’, {people: people});

Browser support

Download a browser build from the latest release, and use it in a script tag.

<script src=”ejs.js”></script>

<script>

let people = [‘geddy’, ‘neil’, ‘alex’],

html = ejs.render(‘<%= people.join(“, “); %>’, {people: people});

</script>

Options

  • cache Compiled functions are cached, requires filename
  • filename Used by cache to key caches, and for includes
  • context Function execution context
  • compileDebug When false no debug instrumentation is compiled
  • client Returns standalone compiled function
  • delimiter Character to use with angle brackets for open/close
  • debug Output generated function body
  • _with Whether or not to use with() {} constructs. If false then the locals will be stored in the locals object.
  • localsName Name to use for the object storing local variables when not using with Defaults to locals
  • rmWhitespace Remove all safe-to-remove whitespace, including leading and trailing whitespace. It also enables a safer version of -%> line slurping for all scriptlet tags (it does not strip new lines of tags in the middle of a line).
  • escape The escaping function used with <%= construct. It is used in rendering and is .toString()ed in the generation of client functions. (By default escapes XML).
  • outputFunctionName Set to a string (e.g., ‘echo’ or ‘print’) for a function to print output inside scriptlet tags.
  • async When true, EJS will use an async function for rendering. (Depends on async/await support in the JS runtime.

Tags

  • <% ‘Scriptlet’ tag, for control-flow, no output
  • <%_ ‘Whitespace Slurping’ Scriptlet tag, strips all whitespace before it
  • <%= Outputs the value into the template (HTML escaped)
  • <%- Outputs the unescaped value into the template
  • <%# Comment tag, no execution, no output
  • <%% Outputs a literal ‘<%’
  • %> Plain ending tag
  • -%> Trim-mode (‘newline slurp’) tag, trims following newline
  • _%> ‘Whitespace Slurping’ ending tag, removes all whitespace after it

Includes

Includes are relative to the template with the include call. (This requires the ‘filename’ option.) For example if you have “./views/users.ejs” and “./views/user/show.ejs” you would use <%- include(‘user/show’); %>.

You’ll likely want to use the raw output tag (<%-) with your include to avoid double-escaping the HTML output.

<ul>

<% users.forEach(function(user){ %>

<%- include(‘user/show’, {user: user}); %>

<% }); %>

</ul>

Caching

EJS ships with a basic in-process cache for caching the intermediate JavaScript functions used to render templates. It’s easy to plug in LRU caching using Node’s `lru-cache` library:

let ejs = require(‘ejs’),

LRU = require(‘lru-cache’);

ejs.cache = LRU(100); // LRU cache with 100-item limit

If you want to clear the EJS cache, call ejs.clearCache. If you’re using the LRU cache and need a different limit, simple reset `ejs.cache` to a new instance of the LRU.

Layouts

EJS does not specifically support blocks, but layouts can be implemented by including headers and footers, like so:

<%- include(‘header’); -%>

<h1>

Title

</h1>

<p>

My page

</p>

<%- include(‘footer’); -%>

Client-side support

Go to the latest release, download ./ejs.js or ./ejs.min.js. Alternately, you can compile it yourself by cloning the repository and running jake build (or $(npm bin)/jake build if jake is not installed globally).

Include one of these files on your page, and ejs should be available globally

Example

<div id=”output”></div>

<script src=”ejs.min.js”></script>

<script>

let people = [‘geddy’, ‘neil’, ‘alex’],

html = ejs.render(‘<%= people.join(“, “); %>’, {people: people});

// With jQuery:

$(‘#output’).html(html);

// Vanilla JS:

document.getElementById(‘output’).innerHTML = html;

</script>

Share this post
[social_warfare]
Using Pug
Node.js Security

Get industry recognized certification – Contact us

keyboard_arrow_up