Roo/XTemplate.js
[roojs1] / Roo / XTemplate.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11 Roo.XTemplate = function(){
12     Roo.XTemplate.superclass.constructor.apply(this, arguments);
13     var s = this.html;
14
15     s = ['<tpl>', s, '</tpl>'].join('');
16
17     var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
18
19     var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;
20     var ifRe   = /^<tpl\b[^>]*?if="(.*?)"/;
21     var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;
22     var m, id = 0;
23     var tpls = [];
24
25     while(m = s.match(re)){
26        var m2 = m[0].match(nameRe);
27        var m3 = m[0].match(ifRe);
28        var m4 = m[0].match(execRe);
29        var exp = null, fn = null, exec = null;
30        var name = m2 && m2[1] ? m2[1] : '';
31        if(m3){
32            exp = m3 && m3[1] ? m3[1] : null;
33            if(exp){
34                fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
35            }
36        }
37        if(m4){
38            exp = m4 && m4[1] ? m4[1] : null;
39            if(exp){
40                exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
41            }
42        }
43        if(name){
44            switch(name){
45                case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
46                case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
47                default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
48            }
49        }
50        tpls.push({
51             id: id,
52             target: name,
53             exec: exec,
54             test: fn,
55             body: m[1]||''
56         });
57        s = s.replace(m[0], '{xtpl'+ id + '}');
58        ++id;
59     }
60     for(var i = tpls.length-1; i >= 0; --i){
61         this.compileTpl(tpls[i]);
62     }
63     this.master = tpls[tpls.length-1];
64     this.tpls = tpls;
65 };
66
67
68 Roo.extend(Roo.XTemplate, Roo.Template, {
69
70     re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
71
72     applySubTemplate : function(id, values, parent){
73         var t = this.tpls[id];
74         if(t.test && !t.test.call(this, values, parent)){
75             return '';
76         }
77         if(t.exec && t.exec.call(this, values, parent)){
78             return '';
79         }
80         var vs = t.target ? t.target.call(this, values, parent) : values;
81         parent = t.target ? values : parent;
82         if(t.target && vs instanceof Array){
83             var buf = [];
84             for(var i = 0, len = vs.length; i < len; i++){
85                 buf[buf.length] = t.compiled.call(this, vs[i], parent);
86             }
87             return buf.join('');
88         }
89         return t.compiled.call(this, vs, parent);
90     },
91
92     compileTpl : function(tpl){
93         var fm = Roo.util.Format;
94         var useF = this.disableFormats !== true;
95         var sep = Roo.isGecko ? "+" : ",";
96         var fn = function(m, name, format, args){
97             if(name.substr(0, 4) == 'xtpl'){
98                 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
99             }
100             var v;
101             if(name.indexOf('.') != -1){
102                 v = name;
103             }else{
104                 v = "values['" + name + "']";
105             }
106             if(format && useF){
107                 args = args ? ',' + args : "";
108                 if(format.substr(0, 5) != "this."){
109                     format = "fm." + format + '(';
110                 }else{
111                     format = 'this.call("'+ format.substr(5) + '", ';
112                     args = ", values";
113                 }
114             }else{
115                 args= ''; format = "("+v+" === undefined ? '' : ";
116             }
117             return "'"+ sep + format + v + args + ")"+sep+"'";
118         };
119         var body;
120         // branched to use + in gecko and [].join() in others
121         if(Roo.isGecko){
122             body = "tpl.compiled = function(values, parent){ return '" +
123                    tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
124                     "';};";
125         }else{
126             body = ["tpl.compiled = function(values, parent){ return ['"];
127             body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
128             body.push("'].join('');};");
129             body = body.join('');
130         }
131         /** eval:var:zzzzzzz */
132         eval(body);
133         return this;
134     },
135
136     applyTemplate : function(values){
137         return this.master.compiled.call(this, values, {});
138         //var s = this.subs;
139     },
140
141     apply : function(){
142         return this.applyTemplate.apply(this, arguments);
143     },
144
145     compile : function(){return this;}
146 });
147
148 Roo.XTemplate.from = function(el){
149     el = Roo.getDom(el);
150     return new Roo.XTemplate(el.value || el.innerHTML);
151 };