Skip to content

Latest commit

 

History

History
85 lines (55 loc) · 1.87 KB

readme.md

File metadata and controls

85 lines (55 loc) · 1.87 KB

pixie

Tiny template engine (422 bytes uglified and gziped)

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

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

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

Install

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
// 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
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.

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.

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

License

MIT © Jamen Marz