Skip to content

Commit

Permalink
feat: add example for using Eta with custom options
Browse files Browse the repository at this point in the history
  • Loading branch information
exoup committed Sep 30, 2023
1 parent a7129dc commit eabfa24
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/eta/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// npm install express
const path = require('node:path');
const express = require('express');
const cons = require('../../');

// Example of declaring eta with custom options.
const eta = new (require('eta').Eta)({
// Have to let Express handle the views directory instead.
views: '.',
varName: 'that',
autoFilter: true,
filterFunction(val) {
if (typeof val === 'string') {
return val.toUpperCase();
}
}
});

const app = express();

cons.requires.eta = eta;
app.engine('eta', cons.eta);
app.set('view engine', 'eta');
app.set('views', path.join(__dirname, './views'));

const users = [];
users.push({ name: 'tobi' }, { name: 'loki' }, { name: 'jane' });

app.get('/', function (req, res) {
res.render('index', {
title: 'Consolidate.js'
});
});

app.get('/users', function (req, res) {
res.render('users', {
title: 'Users',
users
});
});

app.listen(3000);
console.log('Express server listening on port 3000');
5 changes: 5 additions & 0 deletions examples/eta/views/index.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1><%= that.title %></h1>
<p>Welcome to the <%= that.title %> demo. Click a link:</p>
<ul>
<li><a href="/users">/users</a></li>
</ul>
7 changes: 7 additions & 0 deletions examples/eta/views/users.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1><%= that.title %></h1>
<ul>
<% that.users.forEach((user) => { %>
<li><%= user.name %></li>
<% /* You can't see me */ %>
<% }); %>
</ul>

0 comments on commit eabfa24

Please sign in to comment.