Project

General

Profile

1
/* handlebars helpers */
2

    
3
//  moment syntax example: moment(Date("2011-07-18T15:50:52")).format("MMMM YYYY")
4
//  usage: {{dateFormat creation_date format="MMMM YYYY"}}
5
Handlebars.registerHelper('dateFormat', function(context, block) {
6
    if (window.moment) {
7
        var f = block.hash.format || "MMM DD, YYYY hh:mm:ss A";
8
        return moment(context).format(f); //had to remove Date(context)
9
    } else {
10
        return context; //  moment plugin not available. return data as is.
11
    }
12
});
13

    
14
// extended "if" block helper
15
// usage {{#ifCond var1 '==' var2}}
16
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
17
    switch (operator) {
18
        case '==':
19
            return (v1 == v2) ? options.fn(this) : options.inverse(this);
20
        case '===':
21
            return (v1 === v2) ? options.fn(this) : options.inverse(this);
22
        case '!==':
23
            return (v1 !== v2) ? options.fn(this) : options.inverse(this);
24
        case '<':
25
            return (v1 < v2) ? options.fn(this) : options.inverse(this);
26
        case '<=':
27
            return (v1 <= v2) ? options.fn(this) : options.inverse(this);
28
        case '>':
29
            return (v1 > v2) ? options.fn(this) : options.inverse(this);
30
        case '>=':
31
            return (v1 >= v2) ? options.fn(this) : options.inverse(this);
32
        case '&&':
33
            return (v1 && v2) ? options.fn(this) : options.inverse(this);
34
        case '||':
35
            return (v1 || v2) ? options.fn(this) : options.inverse(this);
36
        default:
37
            return options.inverse(this);
38
    }
39
});
40

    
41
/**
42
 * The {{#exists}} helper checks if a variable is defined.
43
 */
44
Handlebars.registerHelper('exists', function(variable, options) {
45
    if (typeof variable !== 'undefined') {
46
        return options.fn(this);
47
    } else {
48
        return options.inverse(this);
49
    }
50
});
(11-11/27)