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, {
21 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
28 s = ['<tpl>', s, '</tpl>'].join('');
30 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
32 var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;
33 var ifRe = /^<tpl\b[^>]*?if="(.*?)"/;
34 var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;
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);
45 var name = m2 && m2[1] ? m2[1] : '';
47 // if - puts fn into test..
48 exp = m3 && m3[1] ? m3[1] : null;
50 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
54 // exec - calls a function... returns empty if true is returned.
55 exp = m4 && m4[1] ? m4[1] : null;
57 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
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+'; }');
74 s = s.replace(m[0], '{xtpl'+ id + '}');
77 for(var i = tpls.length-1; i >= 0; --i){
78 this.compileTpl(tpls[i]);
80 this.master = tpls[tpls.length-1];
85 applySubTemplate : function(id, values, parent){
86 var t = this.tpls[id];
87 if(t.test && !t.test.call(this, values, parent)){
90 if(t.exec && t.exec.call(this, values, parent)){
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){
97 for(var i = 0, len = vs.length; i < len; i++){
98 buf[buf.length] = t.compiled.call(this, vs[i], parent);
102 return t.compiled.call(this, vs, parent);
105 compileTpl : function(tpl)
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){
113 format= 'htmlEncode';
115 if (format == 'raw' ) {
119 if(name.substr(0, 4) == 'xtpl'){
120 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
123 if(name.indexOf('.') != -1){
126 v = "values['" + name + "']";
129 args = args ? ',' + args : "";
130 if(format.substr(0, 5) != "this."){
131 format = "fm." + format + '(';
133 format = 'this.call("'+ format.substr(5) + '", ';
138 format = "("+v+" === undefined ? '' : ";
140 return "'"+ sep + format + v + args + ")"+sep+"'";
143 // branched to use + in gecko and [].join() in others
145 body = "tpl.compiled = function(values, parent){ return '" +
146 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
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('');
155 /** eval:var:zzzzzzz */
157 Roo.log(body.replace(/\\n/,'\n'));
162 applyTemplate : function(values){
163 return this.master.compiled.call(this, values, {});
168 return this.applyTemplate.apply(this, arguments);
173 Roo.XTemplate.from = function(el){
175 return new Roo.XTemplate(el.value || el.innerHTML);