lodash-express
Use Lodash templates easily in Express.
(Note: This is a very minor variation of Casey Foster's underscore-express.)
Install
This package is registered in npm as lodash-express
, so a simple...
npm install lodash-express
...will do it.
Usage
In your Express app setup...
// replace 'html' below with your favorite template extensionvar app = ;app 'html'; app;
...and that's it!
Including Subtemplates
lodash-express
comes with a baked-in include method. Here's an example...
views/header.html
Header!
views/footer.html
views/index.html
<%= include('header') %> Welcome to my homepage!<%= include('footer') %>
app.js
res;
RESULT
Header! Welcome to my homepage!
include
is relative to the file it is called from. Feel free to use relative paths like ../../some/other/subtemplate
.
Using lodash in your templates
By default, lodash-express uses the minimal template module (lodash.template). If you would like to use lodash within your templates, you can pass lodash into the template from the controller.
let _ = ;res;
Then, you can use lodash methods within your templates:
<% _.forEach(usernames, function(username) { %> <%= username %> <% }); %>