tmpl-engine
v0.0.4
Published
tmpl-engine is a template engine written in Node.js.
Readme
tmpl-engine
tmpl-engine is a template engine written in Node.js.
Installation
npm install tmpl-engineUsage
Jinja2-Style Templates
Example:
import * as j2 from 'tmpl-engine/jinja2'
const template = `Hello {{ name }}!`;
const context = { name: 'Alice' };
const rendered = j2.build(template, context);
console.log(rendered);
// -> 'Hello Alice!'The following Jinja2-style template syntax is supported:
{{ variable }}const template = `Hello {{ name }}!`; const context = { name: 'Alice' }; const rendered = j2.build(template, context); // -> Hello Alice!{% for item in items %} ... {% endfor %}const template = `{% for item in items %} - {{ item }} {% endfor %}`; const context = { items: ['x', 'y', 'z'] }; const rendered = j2.build(template, context); // -> - x // - y // - z{% if condition %} ... {% elif condition %} ... {% else %} ... {% endif %}const template = `{% if score >= 90 %} Grade: A {% elif score >= 80 %} Grade: B {% else %} Grade: C {% endif %}`; const context = { score: 95 }; const rendered = j2.build(template, context); // -> Grade: A{# comments #}const template = `This is visible text. {# This is a comment and should be ignored #} This is more visible text.`; const context = {}; const rendered = j2.build(template, context); // -> This is visible text. // This is more visible text.
