code
code

Reputation: 6319

What's the purpose of requiring ejs?

In many code examples for ejs, I see code like this:

// ...
const ejs = require('ejs');
// ...
app.set('view engine', 'ejs');
app.get('/', function(req, res) {
    res.render('index');
});
// ...

But it seems like the constant ejs is never used. Then what's the purpose of requiring it?

It doesn't seem to make any difference when I choose to require or not requiring ejs.

Upvotes: 0

Views: 1996

Answers (2)

KonohaNinja
KonohaNinja

Reputation: 89

We can set ejs template by using: app.set('view engine', 'ejs');

And here we don't require any ejs import.

But situation can come where you to have render some ejs file and then pass on data and do some stuff with this template, then in those scenario you have to require ejs and then use its methods for it. For example:

// Template example for ejs:

const ejs = require('ejs');
ejs.renderFile(
path.join(__dirname, '../../views/pages/your_ejs_file.ejs'),
your_data,
);

That's just one use case. https://www.npmjs.com/package/ejs

Upvotes: 2

kedar sedai
kedar sedai

Reputation: 1722

You are setting the EJS Template Engine by:

app.set('view engine', 'ejs');

But you can also use the ejs for performing caching operation like:

const ejs = require('ejs'),
const LRU = require('lru-cache');
ejs.cache = LRU(100); // LRU cache with 100-item limit

EJS is used to cache the JS function used to render templatees. And if want to clear the EJS caache you can simply call the ejs.clearCache. This is just an example but you can use ejs in many ways. More about Ejs

Upvotes: 1

Related Questions