<%- include('header'); -%>
<h1>
Title
</h1>
<p>
My page
</p>
<%- include('footer'); -%>
Docs
Layouts
Custom file loader
let ejs = require('ejs');
let myFileLoader = function (filePath) {
return 'myFileLoader: ' + fs.readFileSync(filePath);
};
ejs.fileLoader = myFileLoader;
Caching
let ejs = require('ejs'),
LRU = require('lru-cache');
// LRU cache with 100-item limit
ejs.cache = LRU(100);
Custom delimiters
let ejs = require('ejs'),
users = ['geddy', 'neil', 'alex'];
// Just one template
ejs.render('<?= users.join(" | "); ?>',
{users: users},
{delimiter: '?'});
// => 'geddy | neil | alex'
// Or globally
ejs.delimiter = '$';
ejs.render('<$= users.join(" | "); $>',
{users: users});
// => 'geddy | neil | alex'
Using loops
<% if (userLoggedIn) { %>
<p>Welcome, <%= username %>!</p>
<% } else { %>
<p>Please log in.</p>
<% } %>
Conditionals
<% if (userLoggedIn) { %>
<p>Welcome, <%= username %>!</p>
<% } else { %>
<p>Please log in.</p>
<% } %>
Comments