pixie
v2.0.0
Published
Tiny template engine
Downloads
51
Maintainers
Readme
Tiny template engine
var pixie = require('pixie')
// Parse a template
var template = pixie.parse('foo {{bar}} baz', '{{', '}}')
// => [['foo ', ' baz'], ['bar']]
// Compile (using simple default)
pixie.compile(template, { bar: 'Baaar!' })
// => 'foo Baaar! baz'
Pixie is a tiny template engine (321 bytes uglified and 27 SLOC) that creates templates as arrays of strings. This lets you use alternative compilers to support only the syntax you want, precompile templates, or serialize templates as JSON. See the pixie
keyword on npm for more packages.
Installation
$ npm i pixie
Usage
pixie.parse(source, open, close)
Parse the source into a template. This is passed off to a compile
(e.g. pixie.compile
or others)
source
: The template string source being parsedopen
: Tag for opening expressionsclose
: Tag for closing expressions
// Parse with tags
pixie.parse('Hello {{world}} foo {{bar}} baz.', '{{', '}}')
// Parse with alternative tags
pixie.parse('Hello <%world%>!', '<%', '%>')
pixie.compile(template, data)
A simple compiler, substitutes properties from an object by name
template
: A template object that was returned frompixie.parse
data
: An object/array that you want to insert the data into the expressions
// Parse a template
var template = pixie.parse('foo {{bar}} baz {{qux}}')
// Compile template
pixie.compile(template, { bar: 'baaar', qux: 'quuux' })
// => 'foo baaar baz quuux'
Template structure
The template structure is an array, containing two other arrays recognized as [fragments, expressions]
- Expressions: Data between the opening and closing points. In
Foo {{bar}} baz {{qux}}
they would be['bar', 'qux']
- Fragments: Data around your expressions. In the same example, the fragments would be
['Foo ', ' baz', '']
Compilers can choose to interpret and compile these however they choose. The pixie.compile
ones is just a simple point-n-place
License
MIT © Jamen Marz