Skip to content

Commit

Permalink
Merge pull request #2 from exoup/eta-support-1
Browse files Browse the repository at this point in the history
feature: add support for Eta template engine
  • Loading branch information
titanism committed Sep 30, 2023
2 parents 424a693 + 499c634 commit a3b2097
Show file tree
Hide file tree
Showing 7 changed files with 96 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>
38 changes: 38 additions & 0 deletions lib/consolidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,44 @@ exports.ejs.render = function (str, options, cb) {
});
};

/**
* Eta support.
*/

exports.eta = function (path, options, cb) {
return promisify(cb, function (cb) {
try {
const engine =
requires.eta ||
(requires.eta = new (require('eta').Eta)({
views: '.'
}));
cb(null, engine.render(path, options));
} catch (err) {
cb(err);
}
});
};

/**
* Eta string support.
*/

exports.eta.render = function (str, options, cb) {
return promisify(cb, function (cb) {
try {
const engine =
requires.eta ||
(requires.eta = new (require('eta').Eta)({
views: '.'
}));
cb(null, engine.renderString(str, options));
} catch (err) {
cb(err);
}
});
};

/**
* Eco support.
*/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"ejs": "^3.1.9",
"eslint": "8.42.0",
"eslint-config-xo-lass": "2",
"eta": "^3.1.1",
"fixpack": "^4.0.0",
"haml-coffee": "^1.14.1",
"hamlet": "^0.3.3",
Expand Down
1 change: 1 addition & 0 deletions test/consolidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require('./shared/filters').test('liquid-node');
require('./shared/includes').test('liquid-node');

require('./shared').test('ejs');
require('./shared').test('eta');
require('./shared').test('swig');
require('./shared').test('jazz');
require('./shared').test('jqtpl');
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/eta/user.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><%= it.user.name %></p>

0 comments on commit a3b2097

Please sign in to comment.