Negotiable Paleobotanist Missions

npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

pixie

2.0.0 • Public • Published

pixie

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 parsed
  • open: Tag for opening expressions
  • close: 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 from pixie.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


version license downloads/month downloads support me follow

install

npm i pixie

Downloadsweekly downloads

54

version

2.0.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability