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
32 {a_variable} - output encoded.
33 {a_variable.format:("Y-m-d")} - call a method on the variable
34 {a_variable:raw} - unencoded output
35 {a_variable:toFixed(1,2)} - Roo.util.Format."toFixed"
36 {a_variable:this.method_on_template(...)} - call a method on the template object.
41 <tpl for="a_variable or condition.."></tpl>
42 <tpl if="a_variable or condition"></tpl>
43 <tpl exec="some javascript"></tpl>
44 <tpl name="named_template"></tpl> (experimental)
46 <tpl for="."></tpl> - just iterate the property..
47 <tpl for=".."></tpl> - iterates with the parent (probably the template)
51 Roo.XTemplate = function()
53 Roo.XTemplate.superclass.constructor.apply(this, arguments);
60 Roo.extend(Roo.XTemplate, Roo.Template, {
63 * The various sub templates
68 * basic tag replacing syntax
71 * // you can fake an object call by doing this
75 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
78 * compile the template
80 * This is not recursive, so I'm not sure how nested templates are really going to be handled..
87 s = ['<tpl>', s, '</tpl>'].join('');
89 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
90 nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
91 ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
92 execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
93 namedRe = /^<tpl\b[^>]*?name="(\w+)"/, // named templates..
98 while(true == !!(m = s.match(re))){
99 var forMatch = m[0].match(nameRe),
100 ifMatch = m[0].match(ifRe),
101 execMatch = m[0].match(execRe),
102 namedMatch = m[0].match(namedRe),
107 name = forMatch && forMatch[1] ? forMatch[1] : '';
110 // if - puts fn into test..
111 exp = ifMatch && ifMatch[1] ? ifMatch[1] : null;
113 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
118 // exec - calls a function... returns empty if true is returned.
119 exp = execMatch && execMatch[1] ? execMatch[1] : null;
121 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
129 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
130 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
131 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
134 var uid = namedMatch ? namedMatch[1] : id;
138 id: namedMatch ? namedMatch[1] : id,
145 s = s.replace(m[0], '');
147 s = s.replace(m[0], '{xtpl'+ id + '}');
152 for(var i = tpls.length-1; i >= 0; --i){
153 this.compileTpl(tpls[i]);
154 this.tpls[tpls[i].id] = tpls[i];
156 this.master = tpls[tpls.length-1];
160 * same as applyTemplate, except it's done to one of the subTemplates
161 * when using named templates, you can do:
163 * var str = pl.applySubTemplate('your-name', values);
166 * @param {Number} id of the template
167 * @param {Object} values to apply to template
168 * @param {Object} parent (normaly the instance of this object)
170 applySubTemplate : function(id, values, parent)
174 var t = this.tpls[id];
178 if(t.test && !t.test.call(this, values, parent)){
182 Roo.log("Xtemplate.applySubTemplate 'test': Exception thrown");
183 Roo.log(e.toString());
189 if(t.exec && t.exec.call(this, values, parent)){
193 Roo.log("Xtemplate.applySubTemplate 'exec': Exception thrown");
194 Roo.log(e.toString());
199 var vs = t.target ? t.target.call(this, values, parent) : values;
200 parent = t.target ? values : parent;
201 if(t.target && vs instanceof Array){
203 for(var i = 0, len = vs.length; i < len; i++){
204 buf[buf.length] = t.compiled.call(this, vs[i], parent);
208 return t.compiled.call(this, vs, parent);
210 Roo.log("Xtemplate.applySubTemplate : Exception thrown");
211 Roo.log(e.toString());
217 compileTpl : function(tpl)
219 var fm = Roo.util.Format;
220 var useF = this.disableFormats !== true;
221 var sep = Roo.isGecko ? "+" : ",";
222 var undef = function(str) {
223 Roo.log("Property not found :" + str);
227 var fn = function(m, name, format, args)
229 //Roo.log(arguments);
230 args = args ? args.replace(/\\'/g,"'") : args;
231 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
232 if (typeof(format) == 'undefined') {
233 format= 'htmlEncode';
235 if (format == 'raw' ) {
239 if(name.substr(0, 4) == 'xtpl'){
240 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
243 // build an array of options to determine if value is undefined..
245 // basically get 'xxxx.yyyy' then do
246 // (typeof(xxxx) == 'undefined' || typeof(xxx.yyyy) == 'undefined') ?
247 // (function () { Roo.log("Property not found"); return ''; })() :
252 Roo.each(name.split('.'), function(st) {
253 lookfor += (lookfor.length ? '.': '') + st;
254 udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
257 var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; // .. needs )
262 args = args ? ',' + args : "";
264 if(format.substr(0, 5) != "this."){
265 format = "fm." + format + '(';
267 format = 'this.call("'+ format.substr(5) + '", ';
271 return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
275 // called with xxyx.yuu:(test,test)
277 return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
279 // raw.. - :raw modifier..
280 return "'"+ sep + udef_st + name + ")"+sep+"'";
284 // branched to use + in gecko and [].join() in others
286 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
287 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
290 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
291 body.push(tpl.body.replace(/(\r\n|\n)/g,
292 '\\n').replace(/'/g, "\\'").replace(this.re, fn));
293 body.push("'].join('');};};");
294 body = body.join('');
297 Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
299 /** eval:var:tpl eval:var:fm eval:var:useF eval:var:undef */
305 applyTemplate : function(values){
306 return this.master.compiled.call(this, values, {});
311 return this.applyTemplate.apply(this, arguments);
316 Roo.XTemplate.from = function(el){
318 return new Roo.XTemplate(el.value || el.innerHTML);