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     if (this.html) {
14         this.preCompile();
15     }
16 };
17
18
19 Roo.extend(Roo.XTemplate, Roo.Template, {
20
21     re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
22
23     
24     compile: function()
25     {
26         var s = this.html;
27      
28         s = ['<tpl>', s, '</tpl>'].join('');
29     
30         var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
31     
32         var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;
33         var ifRe   = /^<tpl\b[^>]*?if="(.*?)"/;
34         var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;
35         var m, id = 0;
36         var tpls = [];
37     
38         while(true == !!(m = s.match(re))){
39            var m2 = m[0].match(nameRe);
40            var m3 = m[0].match(ifRe);
41            var m4 = m[0].match(execRe);
42            var exp = null,
43                 fn = null,
44                 exec = null;
45            var name = m2 && m2[1] ? m2[1] : '';
46            if(m3){
47                 // if - puts fn into test..
48                 exp = m3 && m3[1] ? m3[1] : null;
49                 if(exp){
50                    fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
51                 }
52            }
53            if(m4){
54                 // exec - calls a function... returns empty if true is  returned.
55                exp = m4 && m4[1] ? m4[1] : null;
56                if(exp){
57                    exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
58                }
59            }
60            if(name){
61                switch(name){
62                    case '.':  name = new Function('values', 'parent', 'with(values){ return values; }'); break;
63                    case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
64                    default:   name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
65                }
66            }
67            tpls.push({
68                 id: id,
69                 target: name,
70                 exec: exec,
71                 test: fn,
72                 body: m[1]||''
73             });
74            s = s.replace(m[0], '{xtpl'+ id + '}');
75            ++id;
76         }
77         for(var i = tpls.length-1; i >= 0; --i){
78             this.compileTpl(tpls[i]);
79         }
80         this.master = tpls[tpls.length-1];
81         this.tpls = tpls;
82         return this;
83     },
84     
85     applySubTemplate : function(id, values, parent){
86         var t = this.tpls[id];
87         if(t.test && !t.test.call(this, values, parent)){
88             return '';
89         }
90         if(t.exec && t.exec.call(this, values, parent)){
91             return '';
92         }
93         var vs = t.target ? t.target.call(this, values, parent) : values;
94         parent = t.target ? values : parent;
95         if(t.target && vs instanceof Array){
96             var buf = [];
97             for(var i = 0, len = vs.length; i < len; i++){
98                 buf[buf.length] = t.compiled.call(this, vs[i], parent);
99             }
100             return buf.join('');
101         }
102         return t.compiled.call(this, vs, parent);
103     },
104
105     compileTpl : function(tpl)
106     {
107         var fm = Roo.util.Format;
108         var useF = this.disableFormats !== true;
109         var sep = Roo.isGecko ? "+" : ",";
110         var fn = function(m, name, format, args){
111             
112             if (!format) {
113                 format= 'htmlEncode';
114             }
115             if (format == 'raw' ) {
116                 format = false;
117             }
118             
119             if(name.substr(0, 4) == 'xtpl'){
120                 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
121             }
122             var v;
123             if(name.indexOf('.') != -1){
124                 v = name;
125             }else{
126                 v = "values['" + name + "']";
127             }
128             if(format && useF){
129                 args = args ? ',' + args : "";
130                 if(format.substr(0, 5) != "this."){
131                     format = "fm." + format + '(';
132                 }else{
133                     format = 'this.call("'+ format.substr(5) + '", ';
134                     args = ", values";
135                 }
136             }else{
137                 args= '';
138                 format = "("+v+" === undefined ? '' : ";
139             }
140             return "'"+ sep + format + v + args + ")"+sep+"'";
141         };
142         var body;
143         // branched to use + in gecko and [].join() in others
144         if(Roo.isGecko){
145             body = "tpl.compiled = function(values, parent){ return '" +
146                    tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
147                     "';};";
148         }else{
149             body = ["tpl.compiled = function(values, parent){ return ['"];
150             body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
151             body.push("'].join('');};");
152             body = body.join('');
153         }
154         
155         /** eval:var:zzzzzzz */
156         eval(body);
157         Roo.log(body.replace(/\\n/,'\n'));
158         
159         return this;
160     },
161
162     applyTemplate : function(values){
163         return this.master.compiled.call(this, values, {});
164         //var s = this.subs;
165     },
166
167     apply : function(){
168         return this.applyTemplate.apply(this, arguments);
169     }
170
171  });
172
173 Roo.XTemplate.from = function(el){
174     el = Roo.getDom(el);
175     return new Roo.XTemplate(el.value || el.innerHTML);
176 };