UNPKG

1.45 kBMarkdownView Raw
1# Anyfetch-Handlebars
2> Visit http://anyfetch.com for details about AnyFetch.
3
4Inject new helpers in handlebars.
5
6# How to install?
7
8```
9npm install
10```
11
12Check everything is working with `npm test`.
13
14# How to use it
15
16```js
17var hbs = require('anyfetch-handlebars');
18hbs.loadAnyfetchHelpers(/* Your context */);
19```
20
21# Example
22
23```js
24var hbs = require('anyfetch-handlebars');
25
26// An example of context
27var rawDocument = {
28 data: {
29 text: 'Test of document'
30 },
31 metadata: {
32 path: '/test/document.txt'
33 },
34 highlight: {}
35};
36
37hbs.loadAnyfetchHelpers(rawDocument);
38
39var compiled = hbs.compile("{ \
40 \"title\": \
41 {{#if metadata.title}} \
42 \"{{attr 'title'}}\" \
43 {{else}} \
44 {{#if metadata.path}} \
45 \"{{extractFilename metadata.path}}\" \
46 {{else}} \
47 \"Unknown document: no path.\" \
48 {{/if}} \
49 {{/if~}}, \
50 \"path\": \"{{attr 'path'}}\", \
51 \"snippet\": \"{{#trim .}}{{shortAttr 'text'}}{{/trim}}\" \
52}");
53
54var json = compiled(rawDocument);
55
56// Beautify JSON
57json = JSON.stringify(JSON.parse(json), null, 4);
58```
59
60This results in:
61
62```json
63{
64 "title": "document",
65 "path": "/test/document.txt",
66 "snippet": "Test of document"
67}
68```
69
70# Contributing to anyfetch-handlebars
71
72This repo is used extensively by AnyFetch.
73If your document-type needs a custom handlebars helper, you're welcome to fork. See [Contributing](CONTRIBUTING.md) for details.
74>>>>>>> master
75
76Support: `support@anyfetch.com`.