forked from tj/consolidate.js
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add example for using Eta with custom options
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |