-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (46 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*!
* handlebars-helper-variable <https://github.com/helpers/handlebars-helper-variable>
*
* Copyright (c) 2015, Brian Woodward.
* Licensed under the MIT License.
*/
'use strict';
/**
* Exports variable helper
*/
module.exports = variable;
/**
* Dyanimcally add JSON objects to the context.
*
* Register the helper with Handlebars
*
* ```js
* var variable = require('handlebars-helper-variable');
* var context = {};
* Handlebars.registerHelper('variable', variable(context));
* ```
*
* Use the helper in a Handlebars template.
*
* ```hbs
* {{#variable "foo"}}
* { "foo": "bar" }
* {{/variable}}
* ```
*
* Compile and render the template with the given context.
*
* ```js
* Handlebars.compile(template)(context);
* //=> context = { foo: { foo: 'bar' } }
* ```
*
* @param {Object} `context` Context object to put the JSON object on.
* @api public
*/
function variable (context) {
return function variableHelper (name, options) {
var content = options.fn(this);
context[name] = JSON.parse(content);
};
}