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">
14 * @class Roo.XTemplate
15 * @extends Roo.Template
16 * Provides a template that can have nested templates for loops or conditionals. The syntax is:
18 var t = new Roo.MasterTemplate(
19 '<select name="{name}">',
20 '<tpl for="options"><option value="{value:trim}">{text:ellipsis(10)}</option></tpl>',
24 // then append, applying the master template values
28 Roo.XTemplate = function(){
29 Roo.XTemplate.superclass.constructor.apply(this, arguments);
36 Roo.extend(Roo.XTemplate, Roo.Template, {
40 * basic tag replacing syntax
43 * // you can fake an object call by doing this
47 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
54 s = ['<tpl>', s, '</tpl>'].join('');
56 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
58 var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;
59 var ifRe = /^<tpl\b[^>]*?if="(.*?)"/;
60 var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;
64 while(true == !!(m = s.match(re))){
65 var m2 = m[0].match(nameRe);
66 var m3 = m[0].match(ifRe);
67 var m4 = m[0].match(execRe);
71 var name = m2 && m2[1] ? m2[1] : '';
73 // if - puts fn into test..
74 exp = m3 && m3[1] ? m3[1] : null;
76 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
80 // exec - calls a function... returns empty if true is returned.
81 exp = m4 && m4[1] ? m4[1] : null;
83 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
89 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
90 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
91 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
101 s = s.replace(m[0], '{xtpl'+ id + '}');
104 for(var i = tpls.length-1; i >= 0; --i){
105 this.compileTpl(tpls[i]);
107 this.master = tpls[tpls.length-1];
112 applySubTemplate : function(id, values, parent)
114 var t = this.tpls[id];
115 if(t.test && !t.test.call(this, values, parent)){
118 if(t.exec && t.exec.call(this, values, parent)){
121 var vs = t.target ? t.target.call(this, values, parent) : values;
122 parent = t.target ? values : parent;
123 if(t.target && vs instanceof Array){
125 for(var i = 0, len = vs.length; i < len; i++){
126 buf[buf.length] = t.compiled.call(this, vs[i], parent);
130 return t.compiled.call(this, vs, parent);
133 compileTpl : function(tpl)
135 var fm = Roo.util.Format;
136 var useF = this.disableFormats !== true;
137 var sep = Roo.isGecko ? "+" : ",";
138 var fn = function(m, name, format, args){
139 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
140 if (typeof(format) == 'undefined') {
141 format= 'htmlEncode';
143 if (format == 'raw' ) {
147 if(name.substr(0, 4) == 'xtpl'){
148 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
152 //if(name.indexOf('.') != -1){
155 // v = "values['" + name + "']";
159 args = args ? ',' + args : "";
161 if(format.substr(0, 5) != "this."){
162 format = "fm." + format + '(';
164 format = 'this.call("'+ format.substr(5) + '", ';
167 return "'"+ sep + format + v + args + ")"+sep+"'";
171 // called with xxyx.yuu:(test,test)
173 return "'"+ sep + "("+v+" === undefined ? '' : " + v + '(' + args + "))"+sep+"'";
175 // raw.. - :raw modifier..
176 return "'"+ sep + "("+v+" === undefined ? '' : " + v + ")"+sep+"'";
180 // branched to use + in gecko and [].join() in others
182 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
183 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
186 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
187 body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
188 body.push("'].join('');};};");
189 body = body.join('');
192 /** eval:var:zzzzzzz */
194 Roo.log(body.replace(/\\n/,'\n'));
199 applyTemplate : function(values){
200 return this.master.compiled.call(this, values, {});
205 return this.applyTemplate.apply(this, arguments);
210 Roo.XTemplate.from = function(el){
212 return new Roo.XTemplate(el.value || el.innerHTML);