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
30 * {a_variable} - output encoded.
31 * {a_variable.format:("Y-m-d")} - call a method on the variable
32 * {a_variable:raw} - unencoded output
33 * {a_variable:toFixed(1,2)} - Roo.util.Format."toFixed"
34 * {a_variable:this.method_on_template(...)} - call a method on the template object.
39 Roo.XTemplate = function()
41 Roo.XTemplate.superclass.constructor.apply(this, arguments);
48 Roo.extend(Roo.XTemplate, Roo.Template, {
52 * basic tag replacing syntax
55 * // you can fake an object call by doing this
59 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
66 s = ['<tpl>', s, '</tpl>'].join('');
68 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
69 nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
70 ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
71 execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
76 while(true == !!(m = s.match(re))){
77 var m2 = m[0].match(nameRe);
78 var m3 = m[0].match(ifRe);
79 var m4 = m[0].match(execRe);
83 var name = m2 && m2[1] ? m2[1] : '';
85 // if - puts fn into test..
86 exp = m3 && m3[1] ? m3[1] : null;
88 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
92 // exec - calls a function... returns empty if true is returned.
93 exp = m4 && m4[1] ? m4[1] : null;
95 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
101 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
102 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
103 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
113 s = s.replace(m[0], '{xtpl'+ id + '}');
116 for(var i = tpls.length-1; i >= 0; --i){
117 this.compileTpl(tpls[i]);
119 this.master = tpls[tpls.length-1];
124 applySubTemplate : function(id, values, parent)
126 var t = this.tpls[id];
127 if(t.test && !t.test.call(this, values, parent)){
130 if(t.exec && t.exec.call(this, values, parent)){
133 var vs = t.target ? t.target.call(this, values, parent) : values;
134 parent = t.target ? values : parent;
135 if(t.target && vs instanceof Array){
137 for(var i = 0, len = vs.length; i < len; i++){
138 buf[buf.length] = t.compiled.call(this, vs[i], parent);
142 return t.compiled.call(this, vs, parent);
145 compileTpl : function(tpl)
147 var fm = Roo.util.Format;
148 var useF = this.disableFormats !== true;
149 var sep = Roo.isGecko ? "+" : ",";
150 var fn = function(m, name, format, args){
151 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
152 if (typeof(format) == 'undefined') {
153 format= 'htmlEncode';
155 if (format == 'raw' ) {
159 if(name.substr(0, 4) == 'xtpl'){
160 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
164 //if(name.indexOf('.') != -1){
167 // v = "values['" + name + "']";
171 args = args ? ',' + args : "";
173 if(format.substr(0, 5) != "this."){
174 format = "fm." + format + '(';
176 format = 'this.call("'+ format.substr(5) + '", ';
179 return "'"+ sep + format + v + args + ")"+sep+"'";
183 // called with xxyx.yuu:(test,test)
185 return "'"+ sep + "("+v+" === undefined ? '' : " + v + '(' + args + "))"+sep+"'";
187 // raw.. - :raw modifier..
188 return "'"+ sep + "("+v+" === undefined ? '' : " + v + ")"+sep+"'";
192 // branched to use + in gecko and [].join() in others
194 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
195 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
198 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
199 body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
200 body.push("'].join('');};};");
201 body = body.join('');
204 /** eval:var:zzzzzzz */
206 Roo.log(body.replace(/\\n/,'\n'));
211 applyTemplate : function(values){
212 return this.master.compiled.call(this, values, {});
217 return this.applyTemplate.apply(this, arguments);
222 Roo.XTemplate.from = function(el){
224 return new Roo.XTemplate(el.value || el.innerHTML);