https://github.com/jamen/pixie
Tiny template functions.
https://github.com/jamen/pixie
engine fast fragments parse pixie small template templating tiny
Last synced: about 2 months ago
JSON representation
Tiny template functions.
- Host: GitHub
- URL: https://github.com/jamen/pixie
- Owner: jamen
- License: mit
- Created: 2016-04-08T23:11:08.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-04-05T03:26:37.000Z (over 7 years ago)
- Last Synced: 2025-01-12T09:42:33.711Z (10 months ago)
- Topics: engine, fast, fragments, parse, pixie, small, template, templating, tiny
- Language: JavaScript
- Homepage:
- Size: 104 KB
- Stars: 14
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
> 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/)