https://github.com/samthor/html-template-compiler
HTML Template Compiler
https://github.com/samthor/html-template-compiler
Last synced: about 1 year ago
JSON representation
HTML Template Compiler
- Host: GitHub
- URL: https://github.com/samthor/html-template-compiler
- Owner: samthor
- License: apache-2.0
- Created: 2024-09-18T03:12:33.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-03T02:18:37.000Z (over 1 year ago)
- Last Synced: 2025-01-18T15:52:50.231Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 55.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A simple HTML template compiler that generates compiled templates which are environment-agnostic.
(The compiler itself runs in Node.)
## Usage
```bash
$ 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.
```ts
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:
```html
{{content}}
{{object.property.hello}}
```
You can use custom tags to handle conditionals:
```html
{{foo}}
Or else?
```
You can pass e.g., `!foo` to invert the conditional.
Or loops:
```html
No items available
```
You can also use `` within a loop to denote an empty block.
## Unsafe
To include unsafe HTML inside other templates, first mark something as unsafe:
```ts
import { unsafe } from 'html-template-compiler';
const out = renderIndex({ body: unsafe('
hello') });
```
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