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()
30 Roo.XTemplate.superclass.constructor.apply(this, arguments);
37 Roo.extend(Roo.XTemplate, Roo.Template, {
41 * basic tag replacing syntax
44 * // you can fake an object call by doing this
48 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
55 s = ['<tpl>', s, '</tpl>'].join('');
57 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
59 var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;
60 var ifRe = /^<tpl\b[^>]*?if="(.*?)"/;
61 var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;
65 while(true == !!(m = s.match(re))){
66 var m2 = m[0].match(nameRe);
67 var m3 = m[0].match(ifRe);
68 var m4 = m[0].match(execRe);
72 var name = m2 && m2[1] ? m2[1] : '';
74 // if - puts fn into test..
75 exp = m3 && m3[1] ? m3[1] : null;
77 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
81 // exec - calls a function... returns empty if true is returned.
82 exp = m4 && m4[1] ? m4[1] : null;
84 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
90 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
91 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
92 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
102 s = s.replace(m[0], '{xtpl'+ id + '}');
105 for(var i = tpls.length-1; i >= 0; --i){
106 this.compileTpl(tpls[i]);
108 this.master = tpls[tpls.length-1];
113 applySubTemplate : function(id, values, parent)
115 var t = this.tpls[id];
116 if(t.test && !t.test.call(this, values, parent)){
119 if(t.exec && t.exec.call(this, values, parent)){
122 var vs = t.target ? t.target.call(this, values, parent) : values;
123 parent = t.target ? values : parent;
124 if(t.target && vs instanceof Array){
126 for(var i = 0, len = vs.length; i < len; i++){
127 buf[buf.length] = t.compiled.call(this, vs[i], parent);
131 return t.compiled.call(this, vs, parent);
134 compileTpl : function(tpl)
136 var fm = Roo.util.Format;
137 var useF = this.disableFormats !== true;
138 var sep = Roo.isGecko ? "+" : ",";
139 var fn = function(m, name, format, args){
140 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
141 if (typeof(format) == 'undefined') {
142 format= 'htmlEncode';
144 if (format == 'raw' ) {
148 if(name.substr(0, 4) == 'xtpl'){
149 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
153 //if(name.indexOf('.') != -1){
156 // v = "values['" + name + "']";
160 args = args ? ',' + args : "";
162 if(format.substr(0, 5) != "this."){
163 format = "fm." + format + '(';
165 format = 'this.call("'+ format.substr(5) + '", ';
168 return "'"+ sep + format + v + args + ")"+sep+"'";
172 // called with xxyx.yuu:(test,test)
174 return "'"+ sep + "("+v+" === undefined ? '' : " + v + '(' + args + "))"+sep+"'";
176 // raw.. - :raw modifier..
177 return "'"+ sep + "("+v+" === undefined ? '' : " + v + ")"+sep+"'";
181 // branched to use + in gecko and [].join() in others
183 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
184 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
187 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
188 body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
189 body.push("'].join('');};};");
190 body = body.join('');
193 /** eval:var:zzzzzzz */
195 Roo.log(body.replace(/\\n/,'\n'));
200 applyTemplate : function(values){
201 return this.master.compiled.call(this, values, {});
206 return this.applyTemplate.apply(this, arguments);
211 Roo.XTemplate.from = function(el){
213 return new Roo.XTemplate(el.value || el.innerHTML);