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