kstpl
v1.1.0
Published
Ksike Template Engine
Downloads
68
Maintainers
Readme
KsTpl: Template Compilation Made Easy
KsTpl is a versatile and extensible Node.js library designed for seamless template compilation in various formats, including Markdown, Twig, EJS, and more. It's a part of the robust Ksike ecosystem, ensuring reliability and compatibility with a range of applications.
Key Features:
- Format Agnostic: KsTpl supports multiple template formats, allowing you to work with your preferred syntax effortlessly.
- Extensibility: Easily integrate new template formats by creating custom drivers. KsTpl follows a simple and intuitive driver concept.
- Cache Management: Benefit from an efficient caching system that you can adapt to your needs, ensuring optimal performance.
This library belong to the Ksike ecosystem:
- KsMf - Microframework (WEB, REST API, CLI, Proxy, etc)
- Ksdp - Design Patterns Library (GoF, GRASP, IoC, DI, etc)
- KsCryp - Cryptographic Library (RSA, JWT, x509, HEX, Base64, Hash, etc)
- KsHook - Event Driven Library
- KsEval - Expression Evaluator Library
- KsWC - Web API deployment Library
- KsTpl - Template Engine
- KsDoc - Document Engine
Driver
- Template Simple: String interpolation
- Template Ejs
- Template Twig: based on Twing Library
- Template Twig: based on Twig Library
- Template Markdown
- Template Markdown: based on marked Library
Quick overview
Installation
npm install kstpl
Load the library
const KsTpl = require("kstpl");
Compile: Template Simple Format
const html = KsTpl.compile(
"{{name}}:{{age}}",
{ name: "Mit", age: 15 },
{ driver: "str" }
);
console.log(
html === "Mit:15"
)
Compile: Template Twig Format
const html = KsTpl.compile(
"{{name}}:{{age}}",
{ name: "Mit", age: 15 },
{ driver: "twing" }
);
console.log(
html === "Mit:15"
)
Compile: Template Ejs Format
const html = KsTpl.compile(
'<%= people.join(","); %>',
{ people: ['geddy', 'neil', 'alex'] },
{ driver: "ejs" }
);
console.log(
html === "geddy,neil,alex"
)
Compile: Template Markdown Format
const html = KsTpl.compile(
'# Hello, Markdown!',
null,
{ driver: "markdown" }
);
console.log(
html === "<h1>Hello, Markdown!</h1>\n"
)
Template engine format autodetection
KsTpl.configure({
map: { "md": "markdown", "html": "twing", "twig": "twing", "ejs": "ejs", "htmljs": "ejs" },
path: __dirname,
ext: ""
});
const ejs2html = await KsTpl.render("simple.ejs", { user: { name: "Mit", age: 15 } });
const md2html = await KsTpl.render("linked.md", {}, { page: {}, next: "Highlight" });
const twig2html = await KsTpl.render("simple.twig", {
list: [
{ name: "Mat", age: 3, twig: true },
{ name: "Deg", age: 4, twig: false },
{ name: "Ste", age: 5, twig: true }
]
});