eryn
v0.3.2
Published
Native template engine
Downloads
4
Maintainers
Readme
About
eryn is a native template engine for NodeJS written in C++17.
It was built with high performance and flexibility in mind, such that it can be used for fast server-side rendering.
Getting started
You can install eryn just like any other npm package.
npm i eryn --save
Note: this package includes a declaration file for TypeScript.
If a prebuild is already available for your platform, you can jump straight to quick examples. If not, see below compiling the package.
The list of prebuilds can be found here.
Documentation
For the complete documentation, check the wiki.
Compiling the Package
You'll need to install a C/C++ compiler, as well as CMake.
The package will be compiled when you install it through npm, if a prebuild is not available. Please note that the devDependencies
listed in package.json are required for building the package.
If you're missing either a compiler or CMake, an error will be shown. Make sure you have both, and try again.
For more details, see the wiki.
To manually compile the package, run:
npm run rebuild
...or directly run cmake-js:
npx cmake-js compile
...or globally install cmake-js to be able to run it anywhere:
npm i -g cmake-js
cmake-js compile
Scripts
- install - installs the package and compiles it if no (pre)build is available
- rebuild - compiles the package
- prebuild - generates prebuilds the package (you need to provide your own generator)
- check - checks if a build or prebuild is available (if not, exits with code
1
)
Quick examples
Here's a basic example.
test.js
var path = require("path");
var eryn = require("eryn")();
// Pass the absolute path to the file. Relative paths might not be safe (see the wiki).
var data = eryn.render(path.join(__dirname, "test.eryn"), {
firstName: "Tyler",
lastName: "Bounty"
});
test.eryn
Hello, [|context.firstName|] [|context.lastName|]!
This will be rendered as:
Hello, Tyler Bounty!
Here's a more complex example, which shows a glimpse of what eryn can do.
Note: if you don't like the syntax, see below Changing the syntax.
test.js
var path = require("path");
var eryn = require("eryn")();
var data = eryn.render(path.join(__dirname, "test.eryn"), {
firstName: "Tyler",
lastName: "Bounty",
greeting: "Hey there",
numbers: [10, 20, 30, 40]
});
test.eryn
Welcome, [|context.firstName|] [|context.lastName|]!
[|? context.greeting.length > 5 |]
The greeting has more than 5 characters!
[|end|]
This is a basic loop:
[|@ num : context.numbers|]
Current number: [|num|]
[|end|]
There is also support for components!
[|% comp.eryn : {message: "Hello"} |]
This is some content for the component!
It can use the parent context: [|context.greeting|]
[|end|]
And self-closing components too!
[|% comp2.eryn : {test: "world"} /|]
comp.eryn
This is a component!
It has context which is automatically stringified: [|context|]
...and works as usual: [|context.message|]
And also some content:
[|content|]
comp2.eryn
Hello, [|context.test|]!
This is a self closing component with no content!
The render function will return a Buffer
, containing:
Welcome, Tyler Bounty!
The greeting has more than 5 characters!
This is a basic loop:
Current number: 10
Current number: 20
Current number: 30
Current number: 40
There's also support for components!
This is a component!
It has context which is automatically stringified: {"message":"Hello"}
...and works as usual: Hello
And also some content:
This is some content for the component!
It can use the parent context: Hey there
And self-closing components too!
Hello, world!
This is a self closing component with no content!
You can use this buffer however you want (e.g. write it to a file, use it as-is, etc).
Changing the syntax
If you don't like the default syntax, you can change it by calling the setOptions
function before rendering the file. Here's an example:
eryn.setOptions({
templateStart: "{{",
templateEnd: "}}",
conditionalStart: "if ",
loopStart: "for ",
loopSeparator: " of ",
componentStart: "component ",
componentSeparator: " with ",
componentSelf: " self"
});
eryn.render(...);
Note: you can call the
setOptions
function as many times as you want. Changes will take effect immediately.
The files can now be written using this syntax. Here's how the first file would look:
test.eryn
Welcome, {{context.firstName}} {{context.lastName}}!
{{if context.greeting.length > 5 }}
The greeting has more than 5 characters!
{{end}}
This is a basic loop:
{{for num of context.numbers}}
Current number: {{num}}
{{end}}
There is also support for components!
{{component comp.eryn with {message: "Hello"} }}
This is some content for the component!
It can use the parent context: {{context.greeting}}
{{end}}
And self-closing components too!
{{component comp2.eryn with {test: "world"} self}}
This will give the exact same result.
Note: you have to change the syntax in all files.
Also: change the syntax wisely. Otherwise, you might run into some problems (see here).
Releases
0.3.2 - October 20th, 2022
0.3.1 - October 15th, 2022
0.3.0 - June 14th, 2022
0.2.7 - March 9th, 2021
0.2.6 - December 27th, 2020
0.2.5 - September 6th, 2020
0.2.4 - August 7th, 2020
0.2.3 - August 3rd, 2020
0.2.2 - July 25th, 2020
0.2.1 - July 19th, 2020
0.2.0 - July 3rd, 2020
0.1.0 - May 2nd, 2020
Contributing
See the guidelines here.
License
eryn was created by UnexomWid. It is licensed under the MIT license.
This project uses first-party and third-party dependencies. They are listed below, along with their licenses.
Dependencies
NPM Packages (dev)
- nodejs/node-addon-api - wrapper classes for N-API (MIT License)
- cmake-js/cmake-js - native addon build tool based on CMake (MIT License)
- moxystudio/cross-spawn - cross-platform alternative for spawn (MIT License)
- substack/minimist - argument parser (MIT License)
- sindresorhus/npm-run-path - PATH for locally installed binaries (MIT License)
First-Party (C/C++)
- UnexomWid/BDP - 64 bit packaging format for binary data (MIT License)
- UnexomWid/remem - memory tracking via address mapping (MIT License)
- UnexomWid/timerh - library for measuring execution time (MIT License)
Third-Party (C/C++)
- tronkko/dirent - dirent port for Windows (MIT License)