Skip to content

moremoban/moban-handlebars

Repository files navigation

moban-handlebars

https://codecov.io/github/moremoban/moban-handlebars/coverage.png https://pepy.tech/badge/moban-handlebars/month https://img.shields.io/github/stars/moremoban/moban-handlebars.svg?style=social&maxAge=3600&label=Star

Introduction

With pybars3, this library allow moban users to have handlebars template in their next documentation endeavour.

Quick start

$ moban "<p>{{firstname}} {{lastname}}</p>" --template-type handlebars -d firstname=hello lastname=world

Nested input objects

Given a data.json file with the following content

{
  "person": {
    "firstname": "Yehuda",
    "lastname": "Katz",
  },
}
$ moban --template-type handlebars -c data.json  "{{person.firstname}} {{person.lastname}}"
Handlebars-ing <p>{{first... to moban.output
Handlebarsed 1 file.
$ cat moban.output
Yehuda Katz

For handlebars.js users, yes, the example was copied from handlebarjs.com. The aim is to show off what we can do.

Let's continue with a bit more fancy feature:

$ moban --template-type handlebars -c data.json "{{#with person}}{{firstname}} {{lastname}} {{/with}}"

Moban's way of pybar3 usage:

Let's save the following file a script.py under helper_and_partial folder:

from moban_handlebars.api import Helper, register_partial

register_partial('header', '<h1>People</h1>')


@Helper('list')
def _list(this, options, items):
    result = [u'<ul>']
    for thing in items:
        result.append(u'<li>')
        result.extend(options['fn'](thing))
        result.append(u'</li>')
    result.append(u'</ul>')
    return result

And given data.json reads as the following:

{
    "people":[
        {"name": "Bill", "age": 100},
        {"name": "Bob", "age": 90},
        {"name": "Mark", "age": 25}
    ]
}

Let's invoke handlebar template:

$ moban --template-type hbs -pd helper_and_partial -c data.json "{{>header}}{{#list people}}{{name}} {{age}}{{/list}}"
Handlebars-ing {{>header}... to moban.output
Handlebarsed 1 file.
$ cat moban.output
<h1>People</h1><ul><li>Bill 100</li><li>Bob 90</li><li>Mark 25</li></ul>

Installation

You can install moban-handlebars via pip:

$ pip install moban-handlebars

or clone it and install it:

$ git clone https://github.com/moremoban/moban-handlebars.git
$ cd moban-handlebars
$ python setup.py install