2.3.0 • Published 5 months ago

handlebars-a-la-json v2.3.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 months ago

Handlebars a la JSON

Handlebars is a great and simple templating engine for generating HTML. But what if you need to generate JSON? That's what this package does.

There is integration for partials. Note: the result of every partial must be a valid JSON string.

Installation

Install it using NPM:

npm install --save handlebars-a-la-json

Usage

You basically instantiate the handler and use it. Instead of a string it will return a JSON object.

const { createJsonHandlebars } = "handlebars-a-la-json";

// instantiate JSON version
const handlebars = createJsonHandlebars();

// compile the template
const template = `{ "message": "Hello {{name}}!" }`;
const compiledTemplate = handlebars.compile(template);

// execute the template
const data = { name: 'Quinton "Rampage" Jacksons' };
const obj = compiledTemplate(data);

// obj.message == 'Hello Quinton "Rampage" Jacksons!'

Error handling

It is really easy to create invalid JSON when you are templating. To make things easier, we've included better error messages. The following code will generate invalid JSON:

const handlebars = createJsonHandlebars();
const template = `{
  "blocks": [
    {{#items}}
    { "message": "Hello {{name}}!" }
    {{/items}}
]}`;
const data = { items: [{ name: "Alpha" }, { name: "Beta" }] };
const compiledTemplate = handlebars.compile(template);
compiledTemplate(data);

The error message will be:

SyntaxError: Unexpected token { in JSON at position 88
3:    { "message": "Hello Alpha!" }
4:    { "message": "Hello Beta!" }
-----^
5:  ]

This will help you debug and find out that you're missing a comma between the objects.

Notes

Because of the nature of this implementation you will have to add any helpers to the created object. Doing this will ensure that HTML- and JSON-template won't collide. Because this package will uncache the current Handlebars-package, it needs to be referenced before or after Handlebar setup.

It is better to use the Handlebars.create to rule out any collisions. More info here: http://handlebarsjs.com/reference.html#base-create

Template parsing made easier

To make template parsing easier, we've added a NamedTemplateParser, that can be used to load and compile templates from a directory:

const { createJsonHandlebars, NamedTemplateParser } = "handlebars-a-la-json";

const handlebars = createJsonHandlebars()
const parser = new NamedTemplateParser(handlebars)

// load dir
await parser.loadTemplatesFromDirectory("./test/files/templates/")

// custom
parser.addNamedTemplate("main", '{ "count": {{items.length}}, "list": [{{#items}}{{> include}}{{#unless @last}}, {{/unless}}{{/items}}] }')
parser.addNamedTemplate("include", '"-{{name}}"')

const actual = parser.parse("main", { items: [{ name: "Alpha" }, { name: "Beta" }] })
2.3.0

5 months ago

2.2.1

8 months ago

2.2.0

8 months ago

2.2.3

7 months ago

2.2.2

7 months ago

2.2.4

7 months ago

2.1.0

8 months ago

2.0.0

8 months ago

1.5.8

5 years ago

1.5.6

5 years ago

1.5.3

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.4.17

6 years ago

1.4.16

6 years ago

1.4.14

6 years ago

1.4.13

6 years ago

1.4.12

6 years ago