I’m learning node.js with express and ejs template. While I’m learning some code I found that they could call config in template without sending it as a variable when rendering.
here is a tiny tiny project as a “todo” program,
in its views/layout.html,
I found
<title><%= config.site_name %></title>
But in controllers/todo.js
I see
var config = require('../config');
...
res.render('index', {todos: rows});
obviously it doesn’t send config to ejs, though todo.js required config at first.
so why the ejs renderer can call config while rendering?
TIA
It uses
app.helpers, which exposes data to the view.Example:
is equivalent to doing:
In every call to
render.Edit: There is also
app.dynamicHelpers, which lets you use stuff onreqandresas well:I recommend a read-through of the Express guide, it’s quite short and gives you an overview of what’s available in Express.