6e93578a6b026f75ff8943093a163b28d9aabd48
[mkws-moved-to-github.git] / src / mkws-handlebars.js
1 // Handlebars helpers
2
3 Handlebars.registerHelper('mkws-json', function(obj) {
4   return $.toJSON(obj);
5 });
6
7
8 Handlebars.registerHelper('mkws-paragraphs', function(obj) {
9   var acc = [];
10   for (var i = 0; i < obj.length; i++) {
11     acc.push('<p>', obj[i], '</p>');
12   }
13   return acc.join('');
14 });
15
16
17 Handlebars.registerHelper('mkws-translate', function(s) {
18   return mkws.M(s);
19 });
20
21
22 // We need {{mkws-attr '@name'}} because Handlebars can't parse {{@name}}
23 Handlebars.registerHelper('mkws-attr', function(attrName) {
24   return this[attrName];
25 });
26
27
28 /*
29  * Use as follows: {{#mkws-if-any NAME1 having="NAME2"}}
30  * Applicable when NAME1 is the name of an array
31  * The guarded code runs only if at least one element of the NAME1
32  * array has a subelement called NAME2.
33  */
34 Handlebars.registerHelper('mkws-if-any', function(items, options) {
35   var having = options.hash.having;
36   for (var i in items) {
37     var item = items[i]
38     if (!having || item[having]) {
39       return options.fn(this);
40     }
41   }
42   return "";
43 });
44
45
46 Handlebars.registerHelper('mkws-first', function(items, options) {
47   var having = options.hash.having;
48   for (var i in items) {
49     var item = items[i]
50     if (!having || item[having]) {
51       return options.fn(item);
52     }
53   }
54   return "";
55 });
56
57
58 Handlebars.registerHelper('mkws-commaList', function(items, options) {
59   var out = "";
60
61   for (var i in items) {
62     if (i > 0) out += ", ";
63     out += options.fn(items[i])
64   }
65
66   return out;
67 });
68
69
70 Handlebars.registerHelper('mkws-index1', function(obj) {
71   return obj.data.index + 1;
72 });