3 Embedded JavaScript templates
5 [![Build Status](https://img.shields.io/travis/mde/ejs/master.svg?style=flat)](https://travis-ci.org/mde/ejs)
6 [![Developing Dependencies](https://img.shields.io/david/dev/mde/ejs.svg?style=flat)](https://david-dm.org/mde/ejs#info=devDependencies)
16 * Control flow with `<% %>`
17 * Escaped output with `<%= %>`
18 * Unescaped raw output with `<%- %>`
19 * Trim-mode ('newline slurping') with `-%>` ending tag
20 * Custom delimiters (e.g., use '<? ?>' instead of '<% %>')
23 * Static caching of intermediate JavaScript
24 * Static caching of templates
25 * Complies with the [Express](http://expressjs.com) view system
31 <h2><%= user.name %></h2>
38 var template = ejs.compile(str, options);
40 // => Rendered HTML string
42 ejs.render(str, data, options);
43 // => Rendered HTML string
46 You can also use the shortcut `ejs.render(dataAndOptions);` where you pass
47 everything in a single object. In that case, you'll end up with local variables
48 for all the passed options.
52 - `cache` Compiled functions are cached, requires `filename`
53 - `filename` Used by `cache` to key caches, and for includes
54 - `context` Function execution context
55 - `compileDebug` When `false` no debug instrumentation is compiled
56 - `client` Returns standalone compiled function
57 - `delimiter` Character to use with angle brackets for open/close
58 - `debug` Output generated function body
59 - `_with` Whether or not to use `with() {}` constructs. If `false` then the locals will be stored in the `locals` object.
63 - `<%` 'Scriptlet' tag, for control-flow, no output
64 - `<%=` Outputs the value into the template (HTML escaped)
65 - `<%-` Outputs the unescaped value into the template
66 - `<%#` Comment tag, no execution, no output
67 - `<%%` Outputs a literal '<%'
68 - `%>` Plain ending tag
69 - `-%>` Trim-mode ('newline slurp') tag, trims following newline
73 Includes are relative to the template with the `include` call. (This
74 requires the 'filename' option.) For example if you have "./views/users.ejs" and
75 "./views/user/show.ejs" you would use `<%- include('user/show'); %>`.
77 You'll likely want to use the raw output tag (`<%-`) with your include to avoid
78 double-escaping the HTML output.
82 <% users.forEach(function(user){ %>
83 <%- include('user/show', {user: user}); %>
88 Includes are inserted at runtime, so you can use variables for the path in the
89 `include` call (for example `<%- include(somePath); %>`). Variables in your
90 top-level data object are available to all your includes, but local variables
91 need to be passed down.
93 NOTE: Include preprocessor directives (`<% include user/show %>`) are
98 Custom delimiters can be applied on a per-template basis, or globally:
101 var ejs = require('ejs'),
102 users = ['geddy', 'neil', 'alex'];
105 ejs.render('<?= users.join(" | "); ?>', {users: users}, {delimiter: '?'});
106 // => 'geddy | neil | alex'
110 ejs.render('<$= users.join(" | "); $>', {users: users});
111 // => 'geddy | neil | alex'
116 EJS does not specifically support blocks, but layouts can be implemented by
117 including headers and footers, like so:
121 <%- include('header'); -%>
128 <%- include('footer'); -%>
131 ## Client-side support
133 Go to the [Latest Release](https://github.com/mde/ejs/releases/latest), download
134 `./ejs.js` or `./ejs.min.js`.
136 Include one of these on your page, and `ejs.render(str)`.
140 There are a number of implementations of EJS:
142 * TJ's implementation, the v1 of this library: https://github.com/tj/ejs
143 * Jupiter Consulting's EJS: http://www.embeddedjs.com/
144 * EJS Embedded JavaScript Framework on Google Code: https://code.google.com/p/embeddedjavascript/
145 * Sam Stephenson's Ruby implementation: https://rubygems.org/gems/ejs
146 * Erubis, an ERB implementation which also runs JavaScript: http://www.kuwata-lab.com/erubis/users-guide.04.html#lang-javascript
150 Licensed under the Apache License, Version 2.0
151 (<http://www.apache.org/licenses/LICENSE-2.0>)
154 EJS Embedded JavaScript templates copyright 2112