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.XTemplate(
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.
37 * <tpl for="a_variable or condition.."></tpl>
38 * <tpl if="a_variable or condition"></tpl>
39 * <tpl exec="some javascript"></tpl>
41 * <tpl for="."></tpl> - just iterate the property..
42 * <tpl for=".."></tpl> - iterates with the parent (probably the template)
46 Roo.XTemplate = function()
48 Roo.XTemplate.superclass.constructor.apply(this, arguments);
55 Roo.extend(Roo.XTemplate, Roo.Template, {
59 * basic tag replacing syntax
62 * // you can fake an object call by doing this
66 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
69 * compile the template
71 * This is not recursive, so I'm not sure how nested templates are really going to be handled..
78 s = ['<tpl>', s, '</tpl>'].join('');
80 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
81 nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
82 ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
83 execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
84 namedRe = /^<tpl\b[^>]*?name="(\w+)"/, // named templates..
89 while(true == !!(m = s.match(re))){
90 var forMatch = m[0].match(nameRe),
91 ifMatch = m[0].match(ifRe),
92 execMatch = m[0].match(execRe),
93 namedMatch = m[0].match(namedRe),
98 name = forMatch && forMatch[1] ? forMatch[1] : '';
101 // if - puts fn into test..
102 exp = ifMatch && ifMatch[1] ? ifMatch[1] : null;
104 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
109 // exec - calls a function... returns empty if true is returned.
110 exp = execMatch && execMatch[1] ? execMatch[1] : null;
112 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
120 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
121 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
122 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
125 var uid = namedMatch ? namedMatch[1] : id;
129 id: namedMatch ? namedMatch[1] : id,
136 s = s.replace(m[0], '');
138 s = s.replace(m[0], '{xtpl'+ id + '}');
143 for(var i = tpls.length-1; i >= 0; --i){
144 this.compileTpl(tpls[i]);
145 this.tpls[tpls[i].id] = tpls[i];
147 this.master = tpls[tpls.length-1];
151 * same as applyTemplate, except it's done to one of the subTemplates
152 * @param {Number} id of the template
153 * @param {Object} values to apply to template
154 * @param {Object} parent (normaly the instance of this object)
156 applySubTemplate : function(id, values, parent)
160 var t = this.tpls[id];
164 if(t.test && !t.test.call(this, values, parent)){
168 Roo.log("Xtemplate.applySubTemplate 'test': Exception thrown");
169 Roo.log(e.toString());
175 if(t.exec && t.exec.call(this, values, parent)){
179 Roo.log("Xtemplate.applySubTemplate 'exec': Exception thrown");
180 Roo.log(e.toString());
185 var vs = t.target ? t.target.call(this, values, parent) : values;
186 parent = t.target ? values : parent;
187 if(t.target && vs instanceof Array){
189 for(var i = 0, len = vs.length; i < len; i++){
190 buf[buf.length] = t.compiled.call(this, vs[i], parent);
194 return t.compiled.call(this, vs, parent);
196 Roo.log("Xtemplate.applySubTemplate : Exception thrown");
197 Roo.log(e.toString());
203 compileTpl : function(tpl)
205 var fm = Roo.util.Format;
206 var useF = this.disableFormats !== true;
207 var sep = Roo.isGecko ? "+" : ",";
208 var undef = function(str) {
209 Roo.log("Property not found :" + str);
213 var fn = function(m, name, format, args)
215 //Roo.log(arguments);
216 args = args ? args.replace(/\\'/g,"'") : args;
217 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
218 if (typeof(format) == 'undefined') {
219 format= 'htmlEncode';
221 if (format == 'raw' ) {
225 if(name.substr(0, 4) == 'xtpl'){
226 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
229 // build an array of options to determine if value is undefined..
231 // basically get 'xxxx.yyyy' then do
232 // (typeof(xxxx) == 'undefined' || typeof(xxx.yyyy) == 'undefined') ?
233 // (function () { Roo.log("Property not found"); return ''; })() :
238 Roo.each(name.split('.'), function(st) {
239 lookfor += (lookfor.length ? '.': '') + st;
240 udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
243 var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; // .. needs )
248 args = args ? ',' + args : "";
250 if(format.substr(0, 5) != "this."){
251 format = "fm." + format + '(';
253 format = 'this.call("'+ format.substr(5) + '", ';
257 return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
261 // called with xxyx.yuu:(test,test)
263 return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
265 // raw.. - :raw modifier..
266 return "'"+ sep + udef_st + name + ")"+sep+"'";
270 // branched to use + in gecko and [].join() in others
272 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
273 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
276 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
277 body.push(tpl.body.replace(/(\r\n|\n)/g,
278 '\\n').replace(/'/g, "\\'").replace(this.re, fn));
279 body.push("'].join('');};};");
280 body = body.join('');
283 Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
285 /** eval:var:tpl eval:var:fm eval:var:useF eval:var:undef */
291 applyTemplate : function(values){
292 return this.master.compiled.call(this, values, {});
297 return this.applyTemplate.apply(this, arguments);
302 Roo.XTemplate.from = function(el){
304 return new Roo.XTemplate(el.value || el.innerHTML);