Skip to content

aleclarson/mini-hb

Repository files navigation

mini-hb

npm Build status Coverage status Bundle size Install size Code style: Prettier Donate

TypeScript successor to mini-handlebars

Features

  • ~4kB minified (compared to ~70kB minified handlebars)
  • ~18kB install size (compared to ~5.8MB with handlebars)
  • tiny API
  • template pre-parsing
  • "parse then render" in one call
  • no batteries included (blocks are just local functions)
  • syntax/context plugin system (coming later)
  • 100% test coverage

Usage

import {hb} from 'mini-hb'

// Use an object literal to provide variables/functions to templates.
let context = {
  foo: 'foo',
  test(template, context) {
    // `template` is what your block contains.
    // `context` is the context your block has access to.
    // The return value is coerced to a string (but undefined is ignored).
    return hb(template, context)
  },
}

// Render a template.
let template = '{{ foo }}{{ bar }}'
let result = hb(template, context)

// Bind a template to `hb`
let render = hb.bind(template)
result = render(context) // "foo"

// Bind a context to `hb`
render = hb.bind(context)
result = render(template) // "foo"

// Bind many contexts to `hb`
render = hb.bind(context, { bar: 'bar' }, { foo: '' })
result = render(template) // "bar"

// Merge contexts into a new context. (null and undefined are skipped)
context = hb(context, { bar: 'bar' }, cond ? { foo: true } : null)

See the tests for more details.