Why ? Because the native EJS library does not provides blocks and inheritance. I could use another EJS compliant library or a fork but it does not feels a good solution as EJS may be improved and these solutions may become deprecated.
The promise here is to not change anything from the source code of EJS and still use the library out of the box.
WORK IN PROGRESS / NOT TESTED
You can both use your own instance of EJS or just initialize this library directly :
// using your local EJS version
var ejs = require('ejs-decorator')(
require('ejs')
);
// using the default EJS version shipped with this module
var ejs = require('ejs-decorator');
Sample usage with express :
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('ejs', require('ejs-decorator').__express);
<%_ layout("default") _%>
some texts ....
<%_ block("demo", function() { _%>
some texts ....
<%_ }); _%>
<%- render("demo"); %>
<%_ helper("foo", function(bar) { _%>
Hello <%= bar %> !
<%_ }) _%>
<%= foo("baz") %>
You can also provide your own functions and improve it's functions
var ejs = require('ejs-decorator');
ejs.hookOutput('sample', function(args, locals, options, output) {
return output.replaceAll(args[0], args[1]);
});
And usage :
<%_ sample('\n', 'br') _%>
- line 1
- line 2
...
var ejs = require('ejs-decorator');
ejs.hookCallback('sample', function(args, locals, options, cb) {
// @todo
});
And usage :
- line 1
<%_ sample('\n', 'br', function() { _%>
- line 2
- line 3
<%_ }); _%>
...
MIT