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);
15 s = ['<tpl>', s, '</tpl>'].join('');
17 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
19 var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;
20 var ifRe = /^<tpl\b[^>]*?if="(.*?)"/;
21 var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;
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] : '';
32 exp = m3 && m3[1] ? m3[1] : null;
34 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
38 exp = m4 && m4[1] ? m4[1] : null;
40 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
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+'; }');
57 s = s.replace(m[0], '{xtpl'+ id + '}');
60 for(var i = tpls.length-1; i >= 0; --i){
61 this.compileTpl(tpls[i]);
63 this.master = tpls[tpls.length-1];
66 Roo.extend(Roo.XTemplate, Roo.Template, {
68 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
70 applySubTemplate : function(id, values, parent){
71 var t = this.tpls[id];
72 if(t.test && !t.test.call(this, values, parent)){
75 if(t.exec && t.exec.call(this, values, parent)){
78 var vs = t.target ? t.target.call(this, values, parent) : values;
79 parent = t.target ? values : parent;
80 if(t.target && vs instanceof Array){
82 for(var i = 0, len = vs.length; i < len; i++){
83 buf[buf.length] = t.compiled.call(this, vs[i], parent);
87 return t.compiled.call(this, vs, parent);
90 compileTpl : function(tpl){
91 var fm = Roo.util.Format;
92 var useF = this.disableFormats !== true;
93 var sep = Roo.isGecko ? "+" : ",";
94 var fn = function(m, name, format, args){
95 if(name.substr(0, 4) == 'xtpl'){
96 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
99 if(name.indexOf('.') != -1){
102 v = "values['" + name + "']";
105 args = args ? ',' + args : "";
106 if(format.substr(0, 5) != "this."){
107 format = "fm." + format + '(';
109 format = 'this.call("'+ format.substr(5) + '", ';
113 args= ''; format = "("+v+" === undefined ? '' : ";
115 return "'"+ sep + format + v + args + ")"+sep+"'";
118 // branched to use + in gecko and [].join() in others
120 body = "tpl.compiled = function(values, parent){ return '" +
121 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
124 body = ["tpl.compiled = function(values, parent){ return ['"];
125 body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
126 body.push("'].join('');};");
127 body = body.join('');
129 /** eval:var:zzzzzzz */
134 applyTemplate : function(values){
135 return this.master.compiled.call(this, values, {});
140 return this.applyTemplate.apply(this, arguments);
143 compile : function(){return this;}
146 Roo.XTemplate.from = function(el){
148 return new Roo.XTemplate(el.value || el.innerHTML);