https://github.com/nberlette/templette
Petite template engine written in TypeScript
https://github.com/nberlette/templette
api cjs class compiler dts esm inject mustache placeholder template templette typescript
Last synced: about 2 months ago
JSON representation
Petite template engine written in TypeScript
- Host: GitHub
- URL: https://github.com/nberlette/templette
- Owner: nberlette
- License: mit
- Created: 2022-05-16T00:07:55.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-17T18:35:40.000Z (over 2 years ago)
- Last Synced: 2025-02-27T19:09:42.275Z (over 1 year ago)
- Topics: api, cjs, class, compiler, dts, esm, inject, mustache, placeholder, template, templette, typescript
- Language: JavaScript
- Homepage: https://npm.runkit.com/templette
- Size: 104 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
templette
petite template compilation class
## Getting Started
### 1. Install
```bash
pnpm add -D templette
```
```bash
yarn add --dev templette
```
```bash
npx i -D templette
```
### 2. Import
```js
// es modules
import Templette from 'templette';
```
```js
// commonjs
const Templette = require('templette');
```
### 3. Instantiate
```js
const t = new Templette();
// or
const t = new Templette('Hello {{name}}!');
```
### 4. Compile
```js
// when you've defined the template in the constructor
t.render({name: 'nberlette'});
```
```js
// to define the template on the fly
t.compile('Hello {{name}}!', {name: 'nberlette'});
```
### Or, use the static compile method
```js
Templette.compile('Hello {{name}}!', {name: 'nberlette'});
```
---
## API
static compile(template: Template, values: Values): string
**Templette's main compile method, powered by JavaScript's powerful builtin RegExp engine.**
**Type**: `global function`
**Params**
- template Template - the raw template string we want to compile
- values Values - substitutions to make, either as a generic list (for numbered keys), or as a map-style
object to replace named keys or deep (dot-notation) paths.
**Returns**: string
#### Example
```js
Templette.compile('Hello {{name}}!', {name: 'Nick'});
// Hello Nick!
```
static cleanup(template: Template, substitutions: [string | number | RegExp, any]): string
**Cleanup a template string and remove some inconsistencies.**
**Kind**: global function **Returns**: string - formatted and normalized template string **Params**
- template string - raw unformatted template string
- [substitutions] Record.<string, unknown> - optional map of substitutions to make: each property
name is the search pattern or string, and its value is the replacement string or function.
### `compile(template, values)` ⇒ string
Templette's main compile method, powered by JavaScript's powerful builtin RegExp engine.
**Type**: `global function`
**Params**:
- template Template - the raw template string we want to compile
- values Values - substitutions to make, either as a generic list (for numbered keys), or as a map-style
object to replace named keys or deep (dot-notation) paths.
#### **Example**:
```js
Templette.compile('Hello {{name}}!', {name: 'Nick'});
// Hello Nick!
```
### `render(values)` ⇒ string
Render the template with provided values.
**Type**: `global function`
**Params**:
- values Values - substitutions to make, either as a generic list (for numbered keys), or as a map-style
object to replace named keys or deep (dot-notation) paths.
MIT © [Nicholas Berlette](https://github.com/nberlette) · inspired by
[@lukeed/templite](https://github.com/lukeed/templite)