Skip to content
Template evaluation for Node, efficient and lightweight
JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.codeclimate.yml
.eslintrc.yml
.gitattributes
.gitignore
.travis.yml
LICENSE
README.md
index.js
package-lock.json
package.json

README.md

teval

Build Status Test Coverage Maintainability

  • Easy value substitution via {{property}} syntax
  • Customizable prefix/suffix (match <property>, or "property", or ...)
  • Caches all templates by default for blazing-fast access times
  • Optionally sanitizes substituted values for use in HTML
  • Optionally normalizes line endings
  • Built with Bluebird promises for ease of use

Install

npm install --save teval

Usage

Say your template looks like this:

Hello, {{name}}! You're looking at my {{ adjective }} template!

Then, you could use the following code:

const teval = require("teval");

teval("/absolute/path/to/template.txt", {
    name: "world",
    adjective: "super cool",
}).then((template) => {
    console.log(template);
});

HTML value escaping

teval can escape HTML values for you, reducing the risk of XSS (or similar) to zero. If the template path ends in .html, it does so automatically by default, otherwise you can turn it on by passing the option.

Adding this to the previous example:

teval("/absolute/path/to/template.txt", {
    name: "evil <b>XSS</b> attackers & other fellas",
    adjective: "completely <HTML>-safe",
}, {
    html: true,
}).then((template) => {
    console.log(template);
});

would yield as output:

Hello, evil &lt;b&gt;XSS&lt;/b&gt; attackers &amp; other fellas! You're looking
at my completely &lt;HTML&gt;-safe template!

Line endings

Just like the html option in the previous example, you can specify a lineEndings option. Set it to a string and teval will replace all the line endings in the evaluated template, whatever form they might have, with that string.

Changing the prefix/suffix

The prefix and suffix are what the property names to be matched are enclosed in, so by default {{ and }}. You can change them to anything you like.

Template:

Hello %name%! You're looking at my %adjective% template!

Code:

const teval = require("teval");

teval("/absolute/path/to/template.txt", {
    name: "world",
    adjective: "super cool",
}, {
    prefix: "%",
    suffix: "%",
}).then((template) => {
    console.log(template);
});
You can’t perform that action at this time.