I am using express (web framework for node.js) with ejs. Now I’d like to create my own filter as described on the ejs github page:
To add a filter simply add a method to the .filters object:
ejs.filters.last = function(obj) {
return obj[obj.length - 1];
};
The question: how do I access that ejs object? I have tried (naively) in app.js:
ejs.filters.myfilter = function (obj) {
....
}
which gives me the error:
ReferenceError: ejs is not defined
You need to require ejs in your application and set a custom filter on it, which will be visible for your Express application. Also note how you use a ejs filter in your view
<%=: data_to_be_filtered | your_filter %>.Example application:
app.js
index.ejs (located in /views)
Download the code directly from github: http://github.com/alessioalex/ejs_filters
Fore more information checkout: https://github.com/visionmedia/ejs