Home

Awesome

XEJS

By @angrykoala

npm version Build Status codecov

eXtreme EJS

Xejs allows you to render files with a custom tag-based language using EJS. Useful to create a custom tag-based language for templating, rendering and transpilers applications.

Recursive templating, what could go wrong?

WARNING: Xejs is still unstable and under heavy development, please, if you are using xejs as a dependency in your project use ~ (tilde) instead of ^(caret) in your package.json to avoid unexpected changes until version 1.0 is released.

How does it works

xejs provides a custom renderer utility on top of ejs, allowing you to define your own tags of the type {{ mytag }} with custom delimiters (e.g. << mytag >>).

xejs will then match your custom regex rules (e.g. /[Tt]itle/) and map them to a string to be renderer by ejs using the provided arguments.

Only strict matched tags will be parsed, meaning that even if the opening closing tags exists, it won't be parsed unless the content matches the regex rules. This way almost never will be necessary to escape characters when using xejs.

The original EJS tags of the file (<% %>) will be escaped and won't be rendered by xejs.

Usage

API for xejs > 0.7

const xejs=require('xejs');
const fs=require('fs');

const myRenderer=new xejs({
    options:{
        openTag: "{{",
        closeTag: "}}"
    },
    tokens: [
        [/bold\s(.+)/, "'<b>$1</b>'"],
        [/msg/, "msg"]
    ],
    args:{
        msg: "<p>Hello World</p>"        
    }
});


myRenderer.render("testFile", function(err,file){
    fs.writeFileSync("renderedFile",file);
});

This code will render all {{ bold [my text] }} into html <b> text and {{msg}} into "<p>Hello World</p>"

Xejs constructor parameters

All parameters are part of the object passed to the constructor. All are optional.

Xejs render method

3 different interfaces are provided in a xejsRenderer:

In all 3 methods, a Promise will be returned if no callback is defined. However, the promise won't be returned otherwise.

Examples:

Using the tags delimiters {{ ... }}

Warning: Only simple tags allowed, nested tags and html-based tags not supported

Deprecated API

For xejs<0.7

The following example shows the deprecated usage of xejs for versions before 0.7

This API and documentation is no longer maintained.

var xejs=require('xejs');
var fs= require('fs');


var options={
    openTag: "{{",
    closeTag: "}}",
    commentTag: "#",
    tokens: [
        [/bold\s(.+)/, "'<b>$1</b>'"]
    ]
};


xejs("example/test.ejs",options,{}, function(err,file){
    fs.writeFileSync('example/prueba.html',file);
});

License

Xejs is being developed and maintained as Open-Source software by @angrykoala (https://github.com/angrykoala) licensed under GNU GENERAL PUBLIC LICENSE version 3

The original source code can be found at: https://github.com/angrykoala/xejs