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>
40 * <tpl name="named_template"></tpl>
42 * <tpl for="."></tpl> - just iterate the property..
43 * <tpl for=".."></tpl> - iterates with the parent (probably the template)
47 Roo.XTemplate = function()
49 Roo.XTemplate.superclass.constructor.apply(this, arguments);
56 Roo.extend(Roo.XTemplate, Roo.Template, {
59 * The various sub templates
64 * basic tag replacing syntax
67 * // you can fake an object call by doing this
71 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
74 * compile the template
76 * This is not recursive, so I'm not sure how nested templates are really going to be handled..
83 s = ['<tpl>', s, '</tpl>'].join('');
85 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
86 nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
87 ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
88 execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
89 namedRe = /^<tpl\b[^>]*?name="(\w+)"/, // named templates..
94 while(true == !!(m = s.match(re))){
95 var forMatch = m[0].match(nameRe),
96 ifMatch = m[0].match(ifRe),
97 execMatch = m[0].match(execRe),
98 namedMatch = m[0].match(namedRe),
103 name = forMatch && forMatch[1] ? forMatch[1] : '';
106 // if - puts fn into test..
107 exp = ifMatch && ifMatch[1] ? ifMatch[1] : null;
109 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
114 // exec - calls a function... returns empty if true is returned.
115 exp = execMatch && execMatch[1] ? execMatch[1] : null;
117 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
125 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
126 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
127 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
130 var uid = namedMatch ? namedMatch[1] : id;
134 id: namedMatch ? namedMatch[1] : id,
141 s = s.replace(m[0], '');
143 s = s.replace(m[0], '{xtpl'+ id + '}');
148 for(var i = tpls.length-1; i >= 0; --i){
149 this.compileTpl(tpls[i]);
150 this.tpls[tpls[i].id] = tpls[i];
152 this.master = tpls[tpls.length-1];
156 * same as applyTemplate, except it's done to one of the subTemplates
157 * @param {Number} id of the template
158 * @param {Object} values to apply to template
159 * @param {Object} parent (normaly the instance of this object)
161 applySubTemplate : function(id, values, parent)
165 var t = this.tpls[id];
169 if(t.test && !t.test.call(this, values, parent)){
173 Roo.log("Xtemplate.applySubTemplate 'test': Exception thrown");
174 Roo.log(e.toString());
180 if(t.exec && t.exec.call(this, values, parent)){
184 Roo.log("Xtemplate.applySubTemplate 'exec': Exception thrown");
185 Roo.log(e.toString());
190 var vs = t.target ? t.target.call(this, values, parent) : values;
191 parent = t.target ? values : parent;
192 if(t.target && vs instanceof Array){
194 for(var i = 0, len = vs.length; i < len; i++){
195 buf[buf.length] = t.compiled.call(this, vs[i], parent);
199 return t.compiled.call(this, vs, parent);
201 Roo.log("Xtemplate.applySubTemplate : Exception thrown");
202 Roo.log(e.toString());
208 compileTpl : function(tpl)
210 var fm = Roo.util.Format;
211 var useF = this.disableFormats !== true;
212 var sep = Roo.isGecko ? "+" : ",";
213 var undef = function(str) {
214 Roo.log("Property not found :" + str);
218 var fn = function(m, name, format, args)
220 //Roo.log(arguments);
221 args = args ? args.replace(/\\'/g,"'") : args;
222 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
223 if (typeof(format) == 'undefined') {
224 format= 'htmlEncode';
226 if (format == 'raw' ) {
230 if(name.substr(0, 4) == 'xtpl'){
231 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
234 // build an array of options to determine if value is undefined..
236 // basically get 'xxxx.yyyy' then do
237 // (typeof(xxxx) == 'undefined' || typeof(xxx.yyyy) == 'undefined') ?
238 // (function () { Roo.log("Property not found"); return ''; })() :
243 Roo.each(name.split('.'), function(st) {
244 lookfor += (lookfor.length ? '.': '') + st;
245 udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
248 var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; // .. needs )
253 args = args ? ',' + args : "";
255 if(format.substr(0, 5) != "this."){
256 format = "fm." + format + '(';
258 format = 'this.call("'+ format.substr(5) + '", ';
262 return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
266 // called with xxyx.yuu:(test,test)
268 return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
270 // raw.. - :raw modifier..
271 return "'"+ sep + udef_st + name + ")"+sep+"'";
275 // branched to use + in gecko and [].join() in others
277 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
278 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
281 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
282 body.push(tpl.body.replace(/(\r\n|\n)/g,
283 '\\n').replace(/'/g, "\\'").replace(this.re, fn));
284 body.push("'].join('');};};");
285 body = body.join('');
288 Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
290 /** eval:var:tpl eval:var:fm eval:var:useF eval:var:undef */
296 applyTemplate : function(values){
297 return this.master.compiled.call(this, values, {});
302 return this.applyTemplate.apply(this, arguments);
307 Roo.XTemplate.from = function(el){
309 return new Roo.XTemplate(el.value || el.innerHTML);