Telesy - Type Safe HTML Templating Library using Template Literals
npm install telesy

- We love TypeScript. Telesy gives the type safe for HTML templates.
- No compilation build phase needed. Telesy just works natively on any ES6 compliant web browsers and Node.js.
- 10 times faster than ReactDomServer.renderToString() to generate static HTML code.
- Lightweight. Less than 1KB when minified. No additional dependencies.
TypeScript
``typescript
import {$$, $$$} from "telesy";
interface Context {
name: string;
options: Option[];
}
interface Option {
value: string;
selected: string;
label: string;
}
// language=HTML
const selectRender = (ctx: Context) => $$
;
document.querySelector
`
JavaScript (ES6)
`js
const {$$, $$$} = require("telesy");
// language=HTML
const selectRender = (ctx) => $$
;
document.querySelector("#here").innerHTML = selectRender(context);
`
See telesy.d.ts for detail.
Template variables:
`js
const render = (ctx) => $$
Hello, ${ctx.name}!
;render({name: "Ken"}); // => '
Hello, Ken!
'
`HTML special characters escaped per default for safe:
`js
const render = (ctx) => $$${ctx.html}
;render({html: 'first line
second line'}); // => '
first line<br>second line
'
`HTML special characters unescaped with
$$$ filter function like dangerouslySetInnerHTML does:`js
const render = (ctx) => $$${$$$(ctx.html)}
;render({html: 'first line
second line'}) // => '
first line
second line
'
`Conditional section for plain string:
`js
const render = (ctx) => $$;render({value: 10}); // => '
10'
`Conditional section with
$$$ tag template literals for HTML elements:`js
const render = (ctx) => $$
};render({src: "image.png", hidden: false}); // => '
'
`Loop sections with nested
$$$ tag template literals:`js
// language=HTML
const render = (ctx) => $$| ${col.v} |
;
`FRAGMENT
- Template literal with
$$ tag returns a plain string.
- Template literal with $$$ tag returns an encapsulated Fragment object as below.
- Function call $$(string) returns an HTML escaped string.
- Function call $$(fragment) returns a raw string for the Fragment object given.
- Function call $$$(string) returns a Fragment object for the string given, vice versa.`typescript
interface Fragment {
outerHTML: string;
}
`EMPTY VALUES
Telesy accepts
string, number values and Fragments within the template literals.
It outputs empty string "" when one of null, undefined or false value is given.
Note that it doesn't accept the primitive true value, on the other hand.
Specify strings to output explicitly, instead.`js
// DON'T
const render = (ctx) => $$${ctx.bool};
render({bool: false}); // => '' (the false value cause an empty string)// DO
const render = (ctx) => $$
${ctx.bool ? "YES" : "NO"};
render({bool: true}); // => 'YES'
render({bool: false}); // => 'NO'
``js
// DON'T
const render = (ctx) => $$${ctx.bool || "it is falsy"};
render({bool: false}); // => 'it is falsy'// DO
const render = (ctx) => $$
${!ctx.bool && "it is falsy"};
const render = (ctx) => $$${ctx.bool ? "" : "it is falsy"};
render({bool: true}); // => ''
render({bool: false}); // => 'it is falsy'
`BENCHMARKS
Telesy is fast enough but type safe.
| Library | Type Safe | Ops/Sec | Note |
|-----------------------------------------------------------|------------|---------|-------------------------------------------|
| Telesy | ✅ Safe | 42,389 | Backed by the native template literals |
| common-tags | ✅ Safe | 8,323 | Nested
safeHtml causes trouble |
| React | ✅ Safe | 4,278 | ReactDomServer.renderToString() is slow |
| Mustatte | ❌ N/A | 82,442 | Fastest but type safe NOT supported |
| Hogan.js | ❌ N/A | 74,087 | Last publish: 8 years ago |
| Handlebars.js | ❌ N/A | 54,129 | A popular Mustache library |The benchmark result above is on node v18.12.0, Apple Silicon M1.
MUSTACHE MIGRATION
If your project has good old Mustache templates,
use the bundled CLI command
mustache2telesy to migrate from Mustache to Telesy.`sh
combine multiple template files to a single TypeScript file
./node_modules/.bin/mustache2telesy --trim --guess templates/*.html > templates.tsgive some hints of property types to get more simple code generated
./node_modules/.bin/mustache2telesy --trim --guess --array="items,itemList" --bool="isHidden,selected" --func="getText" templates/*.html > templates.tsif you need a plain CommonJS file instead of ES Module or TypeScript file
./node_modules/.bin/mustache2telesy --trim --guess --cjs templates/*.html > templates.js
`The author is a Mustache user for more than 10 years.
His Mustache-based project was migrated to Telesy/TypeScript just in minutes.
Most of Mustache's basic features would just get transformed by
mustache2telesy` CLI.- https://github.com/kawanet/telesy
- https://www.npmjs.com/package/telesy