Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jamen/pixie

Tiny template functions.
https://github.com/jamen/pixie

engine fast fragments parse pixie small template templating tiny

Last synced: 17 days ago
JSON representation

Tiny template functions.

Awesome Lists containing this project

README

        


pixie

> Tiny template engine (422 bytes uglified and gziped)

```js
const { parse, compile } = require('pixie')

const template = parse('foo {{bar}} baz', '{{', '}}')
// => [['foo ', ' baz'], ['bar']]

compile(template, { bar: 'Baaar!' })
// => 'foo Baaar! baz'
```

## Install

```sh
npm i pixie
```

## Usage

### `parse(source, open, close)`

Converts a string to a template.

- `source`: template string source being parsed
- `open`: tag for opening expressions
- `close`: tag for closing expressions

```js
// Parse with tags
parse('Hello {{world}} foo {{bar}} baz.', '{{', '}}')

// Parse with alternate tags
parse('Hello <%world%>!', '<%', '%>')
```

### `compile(template, data)`

Replaces values from an object by key.

- `template`: template object that was returned from `parse`
- `data`: object or array to insert into the expressions

```js
var template = parse('foo {{bar}} baz {{qux}}')

compile(template, { bar: 'baaar', qux: 'quuux' })
// 'foo baaar baz quuux'
```

### `render(source, data, open, close)`

An alternative to doing `compile(parse(source, open, close), data)`, it is slightly faster and creates no intermediate template.

```js
render('Hello, {{world}}!', { world: 'Earth' }, '{{', '}}')
// 'Hello Earth!'
```

### Template structure

Given some template source:

```
Hello, {{world}}! I am {{person}}.
```

This is parsed into a template as `[fragments, expressions]`. The expressions would be `['world', 'person']`, and the fragments be the data surrounding the expressions `['Hello, ', '! I am ', '.']`. Compilers interpret these to create their output.

### Command-line Interface

The package also includes a CLI. It just parses stdin and compiles stdout.

```sh
pixie --name "John Doe" < template.src.md > template.md
```

## License

MIT © [Jamen Marz](http://jamenmarz.com/)