Add analytics
[bus.git] / busui / owa / modules / base / js / owa.template.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
OWA.template = function(options) {
 
        if (options) {
                this.options = options;
        }
}
 
OWA.template.prototype = {
        
        /**
         * Template cache
         */
        _tmplCache = {},
        
        /**
     * Client side template parser that uses <#= #> and <# code #> expressions.
     * and # # code blocks for template expansion.
     *    
     * @param  str string The text of the template to expand</param>    
     * @param  data mixed Any javascript variable that is to be merged.  
     * @return string  
     */
        parseTemplate = function(str, data) {
        
            var err = "";
            try {
                var func = this._tmplCache[str];
                if (!func) {
                    var strFunc = "var p=[],print=function(){p.push.apply(p,arguments);};" + 
                                          "with(obj){p.push('" +
                                              str.replace(/[\r\t\n]/g, " ")
                                               .replace(/'(?=[^#]*#>)/g, "\t")
                                               .split("'").join("\\'")
                                               .split("\t").join("'")
                                               .replace(/<#=(.+?)#>/g, "',$1,'")
                                               .split("<#").join("');")
                                               .split("#>").join("p.push('")
                                               + "');}return p.join('');";
                                               
                    //alert(strFunc);
                    func = new Function("obj", strFunc);
                    this._tmplCache[str] = func;
                }
                return func(data);
            } catch (e) { err = e.message; }
            return "< # ERROR: " + err.htmlEncode() + " # >";
        }
}