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
25 t.append('my-form', {name: 'my-select'});
27 * A name attribute for the child template is not required if you have only one child
28 * template or you want to refer to them by index.
30 Roo.XTemplate = function(){
31 Roo.XTemplate.superclass.constructor.apply(this, arguments);
38 Roo.extend(Roo.XTemplate, Roo.Template, {
42 * basic tag replacing syntax
45 * // you can fake an object call by doing this
49 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
56 s = ['<tpl>', s, '</tpl>'].join('');
58 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
60 var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;
61 var ifRe = /^<tpl\b[^>]*?if="(.*?)"/;
62 var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;
66 while(true == !!(m = s.match(re))){
67 var m2 = m[0].match(nameRe);
68 var m3 = m[0].match(ifRe);
69 var m4 = m[0].match(execRe);
73 var name = m2 && m2[1] ? m2[1] : '';
75 // if - puts fn into test..
76 exp = m3 && m3[1] ? m3[1] : null;
78 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
82 // exec - calls a function... returns empty if true is returned.
83 exp = m4 && m4[1] ? m4[1] : null;
85 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
91 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
92 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
93 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
103 s = s.replace(m[0], '{xtpl'+ id + '}');
106 for(var i = tpls.length-1; i >= 0; --i){
107 this.compileTpl(tpls[i]);
109 this.master = tpls[tpls.length-1];
114 applySubTemplate : function(id, values, parent)
116 var t = this.tpls[id];
117 if(t.test && !t.test.call(this, values, parent)){
120 if(t.exec && t.exec.call(this, values, parent)){
123 var vs = t.target ? t.target.call(this, values, parent) : values;
124 parent = t.target ? values : parent;
125 if(t.target && vs instanceof Array){
127 for(var i = 0, len = vs.length; i < len; i++){
128 buf[buf.length] = t.compiled.call(this, vs[i], parent);
132 return t.compiled.call(this, vs, parent);
135 compileTpl : function(tpl)
137 var fm = Roo.util.Format;
138 var useF = this.disableFormats !== true;
139 var sep = Roo.isGecko ? "+" : ",";
140 var fn = function(m, name, format, args){
141 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
142 if (typeof(format) == 'undefined') {
143 format= 'htmlEncode';
145 if (format == 'raw' ) {
149 if(name.substr(0, 4) == 'xtpl'){
150 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
154 //if(name.indexOf('.') != -1){
157 // v = "values['" + name + "']";
161 args = args ? ',' + args : "";
163 if(format.substr(0, 5) != "this."){
164 format = "fm." + format + '(';
166 format = 'this.call("'+ format.substr(5) + '", ';
169 return "'"+ sep + format + v + args + ")"+sep+"'";
173 // called with xxyx.yuu:(test,test)
175 return "'"+ sep + "("+v+" === undefined ? '' : " + v + '(' + args + "))"+sep+"'";
177 // raw.. - :raw modifier..
178 return "'"+ sep + "("+v+" === undefined ? '' : " + v + ")"+sep+"'";
182 // branched to use + in gecko and [].join() in others
184 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
185 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
188 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
189 body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
190 body.push("'].join('');};};");
191 body = body.join('');
194 /** eval:var:zzzzzzz */
196 Roo.log(body.replace(/\\n/,'\n'));
201 applyTemplate : function(values){
202 return this.master.compiled.call(this, values, {});
207 return this.applyTemplate.apply(this, arguments);
212 Roo.XTemplate.from = function(el){
214 return new Roo.XTemplate(el.value || el.innerHTML);