ejs-plus-include
v1.0.1
Published
EJS wrapper that supports <%+ include(...) %> as an alias for <%- include(...) %>
Maintainers
Readme
ejs-plus-include
A small wrapper around EJS that lets you use <%+ include(...) %>
as an alternative to the usual <%- include(...) %>.
This is not an official EJS feature — it's just a preprocessor that replaces <%+
with <%- before handing the template off to the original ejs engine.
Installation
npm install ejs-plus-includeUsage
Plain render
const ejsPlus = require('ejs-plus-include');
const html = ejsPlus.render('<%+ include("partials/header") %>', { title: 'Home' });With Express
const express = require('express');
const ejsPlus = require('ejs-plus-include');
const app = express();
app.engine('ejs', ejsPlus.__express);
app.set('view engine', 'ejs');
app.set('views', './views');Then in your .ejs files:
<%+ include('partials/header') %>
<h1><%= title %></h1>
<%+ include('partials/footer') %>This behaves exactly like <%- include(...) %>: the included HTML is not escaped.
Why does this exist?
In vanilla EJS:
| Tag | Behavior |
|---|---|
| <% %> | plain JS code, no output |
| <%= %> | outputs the value, HTML-escaped |
| <%- %> | outputs the value unescaped (used for include, raw HTML, etc.) |
<%+ is not a standard tag. This package adds it as a convenience alias for people
who find + more intuitive for "insert HTML here" than -.
API
render(template, data, options)
Same as ejs.render(), but replaces <%+ with <%- first.
renderFile(path, data, options, callback)
Renders a file from disk, using an Express-compatible callback style.
__express
Can be passed directly to Express's app.engine():
app.engine('ejs', ejsPlus.__express);ejs
The original, unmodified ejs module, in case you need to access it directly.
Caveats
- The substitution is a simple string replace (
<%+→<%-), so if a JS string literal in your template happens to contain the exact sequence<%+, it will also be replaced. - Since this is non-standard EJS syntax, if you work in a team, it's worth documenting (e.g. in this README) so other developers aren't confused by an undocumented tag.
License
MIT
