4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
11 Roo.XTemplate = function(){
12 Roo.XTemplate.superclass.constructor.apply(this, arguments);
19 Roo.extend(Roo.XTemplate, Roo.Template, {
23 * basic tag replacing syntax
26 * // you can fake an object call by doing this
30 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
37 s = ['<tpl>', s, '</tpl>'].join('');
39 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
41 var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;
42 var ifRe = /^<tpl\b[^>]*?if="(.*?)"/;
43 var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;
47 while(true == !!(m = s.match(re))){
48 var m2 = m[0].match(nameRe);
49 var m3 = m[0].match(ifRe);
50 var m4 = m[0].match(execRe);
54 var name = m2 && m2[1] ? m2[1] : '';
56 // if - puts fn into test..
57 exp = m3 && m3[1] ? m3[1] : null;
59 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
63 // exec - calls a function... returns empty if true is returned.
64 exp = m4 && m4[1] ? m4[1] : null;
66 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
72 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
73 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
74 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
84 s = s.replace(m[0], '{xtpl'+ id + '}');
87 for(var i = tpls.length-1; i >= 0; --i){
88 this.compileTpl(tpls[i]);
90 this.master = tpls[tpls.length-1];
95 applySubTemplate : function(id, values, parent)
97 var t = this.tpls[id];
98 if(t.test && !t.test.call(this, values, parent)){
101 if(t.exec && t.exec.call(this, values, parent)){
104 var vs = t.target ? t.target.call(this, values, parent) : values;
105 parent = t.target ? values : parent;
106 if(t.target && vs instanceof Array){
108 for(var i = 0, len = vs.length; i < len; i++){
109 buf[buf.length] = t.compiled.call(this, vs[i], parent);
113 return t.compiled.call(this, vs, parent);
116 compileTpl : function(tpl)
118 var fm = Roo.util.Format;
119 var useF = this.disableFormats !== true;
120 var sep = Roo.isGecko ? "+" : ",";
121 var fn = function(m, name, format, args){
122 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
123 if (typeof(format) == 'undefined') {
124 format= 'htmlEncode';
126 if (format == 'raw' ) {
130 if(name.substr(0, 4) == 'xtpl'){
131 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
135 //if(name.indexOf('.') != -1){
138 // v = "values['" + name + "']";
142 args = args ? ',' + args : "";
144 if(format.substr(0, 5) != "this."){
145 format = "fm." + format + '(';
147 format = 'this.call("'+ format.substr(5) + '", ';
150 return "'"+ sep + format + v + args + ")"+sep+"'";
153 format = "("+v+" === undefined ? '' : ";
155 // called with xxyx.yuu:(test,test)
157 return "'"+ sep + format + v + '(' + args + "))"+sep+"'";
161 return "'"+ sep + format + v + args + ")"+sep+"'";
165 // branched to use + in gecko and [].join() in others
167 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
168 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
171 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
172 body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
173 body.push("'].join('');};};");
174 body = body.join('');
177 /** eval:var:zzzzzzz */
179 Roo.log(body.replace(/\\n/,'\n'));
184 applyTemplate : function(values){
185 return this.master.compiled.call(this, values, {});
190 return this.applyTemplate.apply(this, arguments);
195 Roo.XTemplate.from = function(el){
197 return new Roo.XTemplate(el.value || el.innerHTML);