Nodejs EJS helper functions?
Problem
Is there a way to register Helper Functions to EJS templates, so that they can be called from any EJS template? So, it should work something like this.
app.js
ejs.helpers.sayHi = function(name) {
return 'Hello ' + name;
});
index.ejs
Problem courtesy of: Stephen Bugs Kamenar
Solution
Yes, in Express 3 you can add helpers to app.locals
. Ex:
app.locals.somevar = "hello world";
app.locals.someHelper = function(name) {
return ("hello " + name);
}
These would be accessible inside your views like this:
Note: Express 2.5 did helpers differently.
Solution courtesy of: dylanized
Discussion
View additional discussion.