0.2.0 • Published 4 years ago
lodash-express v0.2.0
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 extension
var app = express();
require('lodash-express')(app, 'html');
app.set('view engine', 'html');
...and that's it!
Including Subtemplates
lodash-express
comes with a baked-in include method. Here's an example...
views/header.html
<html>
<head>
<title>Header!</title>
</head>
<body>
views/footer.html
</body>
</html>
views/index.html
<%= include('header') %>
Welcome to my homepage!
<%= include('footer') %>
app.js
res.render('index');
RESULT
<html>
<head>
<title>Header!</title>
</head>
<body>
Welcome to my homepage!
</body>
</html>
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 _ = require('lodash');
res.render('home', {_:_, message:'Welcome', usernames: ['Adam','Scott'] });
Then, you can use lodash methods within your templates:
<ul>
<% _.forEach(usernames, function(username) { %>
<li><%= username %></li>
<% }); %>
</ul>