template

webix.template(str)

webix.template helper.

Please look into the linked official documentation.

References

helpers
ajax(), assert_error(), uid().

External references

Official documentation page.

Code

  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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
webix.template = function(str){
    if (typeof str == "function") return str;
    if (_cache[str])
        return _cache[str];

    str=(str||"").toString();
    if (str.indexOf("->")!=-1){
        var teststr = str.split("->");
        switch(teststr[0]){
            case "html":    //load from some container on the page
                str = webix.html.getValue(teststr[1]);
                break;
            case "http":    //load from external file
                str = new webix.ajax().sync().get(teststr[1],{uid:webix.uid()}).responseText;
                break;
            default:
                //do nothing, will use template as is
                break;
        }
    }

    //supported idioms
    // {obj.attr} => named attribute or value of sub-tag in case of xml
    str=(str||"").toString();

    // Content Security Policy enabled
    if(webix.env.strict){
        if (!_csp_cache[str]){
            _csp_cache[str] = [];

            // get an array of objects (not sorted by position)
            var temp_res = [];
            str.replace(/\{obj\.([^}?]+)\?([^:]*):([^}]*)\}/g,function(search,s1,s2,s3,pos){
                    temp_res.push({pos: pos, str: search, fn: function(obj,common){
                        return obj[s1]?s2:s3;
                    }});
            });
            str.replace(/\{common\.([^}\(]*)\}/g,function(search,s,pos){
                temp_res.push({pos: pos, str: search, fn: function(obj,common){
                    return common[s]||'';
                }});
            });
            str.replace(/\{common\.([^\}\(]*)\(\)\}/g,function(search,s,pos){
                temp_res.push({pos: pos, str: search, fn: function(obj,common){
                    return (common[s]?common[s].apply(this, arguments):"");
                }});
            });
            str.replace(/\{obj\.([^:}]*)\}/g,function(search,s,pos){
                temp_res.push({pos: pos, str: search, fn: function(obj,common){
                    return obj[s];
                }});
            });
            str.replace("{obj}",function(search,s,pos){
                temp_res.push({pos: pos, str: search, fn: function(obj,common){
                    return obj;
                }});
            });
            str.replace(/#([^#'";, ]+)#/gi,function(search,s,pos){
                if(s.charAt(0)=="!"){
                    temp_res.push({pos: pos, str: search, fn: function(obj,common){
                        s = s.substr(1);
                        if(s.indexOf(".")!= -1)
                            obj = webix.CodeParser.collapseNames(obj); // apply complex properties
                        return webix.template.escape(obj[s.substr(1)]);
                    }});
                }
                else{
                    temp_res.push({pos: pos, str: search, fn: function(obj,common){
                        if(s.indexOf(".")!= -1)
                            obj = webix.CodeParser.collapseNames(obj); // apply complex properties
                        return obj[s];
                    }});
                }

            });

            // sort template parts by position
            temp_res.sort(function(a,b){
                return (a.pos > b.pos)?1:-1;
            });

            // create an array of functions that return parts of html string
            if(temp_res.length){
                var lastPos = 0;
                var addStr = function(str,n0,n1){
                    _csp_cache[str].push(function(){
                        return str.slice(n0,n1);
                    });
                };
                for(var i = 0; i< temp_res.length; i++){
                    var pos = temp_res[i].pos;
                    addStr(str,lastPos,pos);
                    _csp_cache[str].push(temp_res[i].fn);
                    lastPos = pos + temp_res[i].str.length;
                }
                addStr(str,lastPos,str.length);
            }
            else
                _csp_cache[str].push(function(){return str;});
        }
        return function(){
            var s = "";
            for(var i=0; i < _csp_cache[str].length;i++){
                s += _csp_cache[str][i].apply(this,arguments);
            }
            return s;
        };
    }

    str=str.replace(slashes,"\\\\");
    str=str.replace(newlines,"\\n");
    str=str.replace(quotes,"\\\"");

    str=str.replace(/\{obj\.([^}?]+)\?([^:]*):([^}]*)\}/g,"\"+(obj.$1?\"$2\":\"$3\")+\"");
    str=str.replace(/\{common\.([^}\(]*)\}/g,"\"+(common.$1||'')+\"");
    str=str.replace(/\{common\.([^\}\(]*)\(\)\}/g,"\"+(common.$1?common.$1.apply(this, arguments):\"\")+\"");
    str=str.replace(/\{obj\.([^}]*)\}/g,"\"+(obj.$1)+\"");
    str=str.replace("{obj}","\"+obj+\"");
    str=str.replace(/#([^#'";, ]+)#/gi,function(str, key){
        if (key.charAt(0)=="!")
            return "\"+webix.template.escape(obj."+key.substr(1)+")+\"";
        else
            return "\"+(obj."+key+")+\"";
    });

    try {
        _cache[str] = Function("obj","common","return \""+str+"\";");
    } catch(e){
        webix.assert_error("Invalid template:"+str);
    }

    return _cache[str];
};



webix.template.escape  = function(str){
    if (str === webix.undefined || str === null) return "";
    return (str.toString() || "" ).replace(badChars, escapeChar);
};