본문 바로가기

업무관련/JavaScript

Handlebars - registerHelper 모음

 

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
 * handlebarsHelper - for문
 * ex)
 */
Handlebars.registerHelper('for'function(from, to, incr, block) {
    var accum = '';
    for(var i = from; i <= to; i += incr)
        accum += block.fn(i);
    return accum;
});
 
/**
 * handlebarsHelper - index ( start : 1 )
 * ex) {{incremented @index}}
 */
Handlebars.registerHelper('incremented'function(index){
    return index + 1;
});
 
/**
 * handlebarsHelper - if문
 * ex)
 * {{#ifEquals bizArea "A"}}
 * 소계
 * {{else ifEquals bizArea "B"}}
 * 합계
 * {{else}}
 * 누계
 * {{/ifEquals}}
 */
Handlebars.registerHelper('ifEquals'function(arg1, arg2, options) {
    return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});
 
/**
 * handlebarsHelper - select
 */
window.Handlebars.registerHelper('select'function(value, options) {
    var $el = $('<select />').html( options.fn(this) );
    $el.find('[value="' + value + '"]').attr({'selected':'selected'});
    return $el.html();
});
 
/**
 * handlebarsHelper - numberFormat 기본 소수점 절삭
 * 사용법 참조 : http://jsfiddle.net/DennyLoko/6sR87/
 */
Handlebars.registerHelper('numberFormat'function (value, options) {
    // Helper parameters
    var dl = options.hash['decimalLength'|| 0;
    var ts = options.hash['thousandsSep'|| ',';
    var ds = options.hash['decimalSep'|| '.';
 
    // Parse to float
    var value = parseFloat(value);
    if (isNaN(value))
        return '';
 
    // The regex
    var re = '\\d(?=(\\d{3})+' + (dl > 0 ? '\\D' : '$'+ ')';
 
    // Formats the number with the decimals
    var num = value.toFixed(Math.max(0, ~~dl));
 
    // Returns the formatted number
    return (ds ? num.replace('.', ds) : num).replace(new RegExp(re, 'g'), '$&' + ts);
});
 
/**
 * handlebarsHelper - defaultValue 설정
 * ex) {{defaultValue bizArea '값이없음'}}
 */
Handlebars.registerHelper('defaultValue'function (value, defaultValue) {
    var out = value || defaultValue;
    return new Handlebars.SafeString(out);
});
 
/**
 * handlebarsHelper - 부등호 사용 스크립트
 */
Handlebars.registerHelper('ifCond'function (v1, operator, v2, options) {
    switch (operator) {
        case '==':
            return (v1 == v2) ? options.fn(this) : options.inverse(this);
        case '===':
            return (v1 === v2) ? options.fn(this) : options.inverse(this);
        case '!=':
            return (v1 != v2) ? options.fn(this) : options.inverse(this);
        case '!==':
            return (v1 !== v2) ? options.fn(this) : options.inverse(this);
        case '<':
            return (v1 < v2) ? options.fn(this) : options.inverse(this);
        case '<=':
            return (v1 <= v2) ? options.fn(this) : options.inverse(this);
        case '>':
            return (v1 > v2) ? options.fn(this) : options.inverse(this);
        case '>=':
            return (v1 >= v2) ? options.fn(this) : options.inverse(this);
        case '&&':
            return (v1 && v2) ? options.fn(this) : options.inverse(this);
        case '||':
            return (v1 || v2) ? options.fn(this) : options.inverse(this);
        default:
            return options.inverse(this);
    }
});
cs

'업무관련 > JavaScript' 카테고리의 다른 글

JavaScript 소수점 반올림  (0) 2014.01.15
Javascript 특수문자,공백,숫자등 Text 체크  (0) 2013.12.02
ALTER TABLE  (0) 2013.07.25