1 | # Anyfetch-Handlebars
|
2 | > Visit http://anyfetch.com for details about AnyFetch.
|
3 |
|
4 | Inject new helpers in handlebars.
|
5 |
|
6 | # How to install?
|
7 |
|
8 | ```
|
9 | npm install
|
10 | ```
|
11 |
|
12 | Check everything is working with `npm test`.
|
13 |
|
14 | # How to use it
|
15 |
|
16 | ```js
|
17 | var hbs = require('anyfetch-handlebars');
|
18 | hbs.loadAnyfetchHelpers(/* Your context */);
|
19 | ```
|
20 |
|
21 | # Example
|
22 |
|
23 | ```js
|
24 | var hbs = require('anyfetch-handlebars');
|
25 |
|
26 | // An example of context
|
27 | var rawDocument = {
|
28 | data: {
|
29 | text: 'Test of document'
|
30 | },
|
31 | metadata: {
|
32 | path: '/test/document.txt'
|
33 | },
|
34 | highlight: {}
|
35 | };
|
36 |
|
37 | hbs.loadAnyfetchHelpers(rawDocument);
|
38 |
|
39 | var 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 |
|
54 | var json = compiled(rawDocument);
|
55 |
|
56 | // Beautify JSON
|
57 | json = JSON.stringify(JSON.parse(json), null, 4);
|
58 | ```
|
59 |
|
60 | This 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 |
|
72 | This repo is used extensively by AnyFetch.
|
73 | If your document-type needs a custom handlebars helper, you're welcome to fork. See [Contributing](CONTRIBUTING.md) for details.
|
74 | >>>>>>> master
|
75 |
|
76 | Support: `support@anyfetch.com`.
|