Embedded JavaScript (EJS)

EJS is a JavaScript templating engine that works much like Underscore.js and also uses ERB <% %> style delimiters. Alternatively, it also allows the use of [% %] style tags for delimiters.

Just like Underscore.js, EJS allows arbitrary JavaScript to be parsed when used with the standard <% %> ERB style syntax, and allows the evaluation of expressions using an equals sign = following the opening delimiter tag <%= %>:

<ul> 
<% for (var i = 0; i < people.length; i++) { %> 
    <li><%= firstName %> <%= lastName %></li> 
<% } %> 
</ul> 

This template can be used to iterate over a list of objects from which key names are evaluated as variables with different values:

{ "people": [ { "firstName": "Peebo", "lastName": "Sanderson" }, { "firstName": ...

Get Mastering JavaScript Single Page Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.