html-template-compiler
v0.1.2
Published
A simple HTML template compiler that generates compiled templates which are environment-agnostic. (The compiler itself runs in Node.)
Downloads
10
Readme
A simple HTML template compiler that generates compiled templates which are environment-agnostic. (The compiler itself runs in Node.)
Usage
$ npx html-template-compiler templates/
This emits TypeScript code, for the files "templates/*.html", to stdout. (You can use glob-syntax in your shell, too.) The generated code can be called to render templates, and includes relatively comprehensive types.
By default this imports this package's runtime code to render templates.
You can pass flag -i
to inline the code instead.
Either way, use a tree-shaking compiler.
The 'rendered' object is something with a toString()
helper.
import { renderIndex } from './generated-template.ts';
const out = renderIndex({ prop: 'hello', there: 'jim' });
const s = out.toString();
Syntax
This supports simple rendering of passed properties:
<div id="{{idName}}" ?disabled="{{foo}}">{{content}}</div>
{{object.property.hello}}
You can use custom tags to handle conditionals:
<hc:if i="foo">
<div class="foo-is-truthy">{{foo}}</div>
<hc-else />
<div class="else">Or else?</div>
</hc-if>
You can pass e.g., !foo
to invert the conditional.
Or loops:
<hc:loop iter="foo" v="eachFoo">
<div class="each-foo-bar">{{eachFoo.bar}}</div>
</hc:loop>
<hc:if iter="!foo">
<!-- Iterates the first item, don't use on generators -->
<div class="empty">No items available</div>
</hc:if>
You can also use <hc:else />
within a loop to denote an empty block.
Unsafe
To include unsafe HTML inside other templates, first mark something as unsafe:
import { unsafe } from 'html-template-compiler';
const out = renderIndex({ body: unsafe('<div>hello</div>') });
The rendered objects generated by the compiled code are already denoted unsafe.
TODOs
- This does not currently support
Promise
arguments, but it could be modified to do so - It doesn't care or know anything about events or live DOM: this is purely for backend or static generation