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'
npm i pixie
Converts a string to a template.
source
: template string source being parsedopen
: tag for opening expressionsclose
: tag for closing expressions
// Parse with tags
parse('Hello {{world}} foo {{bar}} baz.', '{{', '}}')
// Parse with alternate tags
parse('Hello <%world%>!', '<%', '%>')
Replaces values from an object by key.
template
: template object that was returned fromparse
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'
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!'
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.
The package also includes a CLI. It just parses stdin and compiles stdout.
pixie --name "John Doe" < template.src.md > template.md
MIT © Jamen Marz