Skip to content

ichiriac/ejs-decorator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ejs-decorator

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


Usage

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);

Helpers

Inheritance

<%_ layout("default") _%>
some texts ....

Defining Blocks

<%_ block("demo", function() { _%>
some texts ....
<%_ }); _%>

Using Blocks

<%- render("demo"); %>

Define helpers

<%_ helper("foo", function(bar) { _%>
  Hello <%= bar %> !
<%_ }) _%>

Using helpers

<%= foo("baz") %>

Extending your-self EJS

You can also provide your own functions and improve it's functions

hookOutput

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
...

hookCallback

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
<%_ }); _%>
...

License

MIT

About

Provides extra helpers over the native EJS library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published