Git Product home page Git Product logo

pixie's Introduction

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

pixie's People

Contributors

ahdinosaur avatar jamen avatar vectrixdevelops avatar whaaaley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

seanc ahdinosaur

pixie's Issues

Support nesting.

Supporting nested tags:

Hello {{foo {{bar}} baz}}.

Right now, the expression would be considered:

foo {{bar

I would like it to be:

foo {{bar}} baz

ES6 version

I want to try to make experimental ES6 version at pixie/es6 that has the same usage but relies on things like rests/spreads to be fast.

Different template structure

Right now, a template is:

{
  "partials": [ ...partials ],
  "expressions": [ ...expressions ]
}

Would be nice if it was:

[[ ...partials ], [ ...expressions ]]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.