templette
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

templette

petite template compilation class


Getting Started


1. Install

pnpm add -D templette
yarn add --dev templette
npx i -D templette

2. Import

// es modules
import Templette from 'templette';
// commonjs
const Templette = require('templette');

3. Instantiate

const t = new Templette();
// or
const t = new Templette('Hello {{name}}!');

4. Compile

// when you've defined the template in the constructor
t.render({name: 'nberlette'});
// to define the template on the fly
t.compile('Hello {{name}}!', {name: 'nberlette'});

Or, use the static compile method

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

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:

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 · inspired by @lukeed/templite

Package Sidebar

Install

npm i templette

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

15.3 kB

Total Files

7

Last publish

Collaborators

  • berlette