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,
73 s = ['<tpl>', s, '</tpl>'].join('');
75 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
76 nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
77 ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
78 execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
79 namedRe = /^<tpl\b[^>]*?name="(\w+)"/, // named templates..
84 while(true == !!(m = s.match(re))){
85 var m2 = m[0].match(nameRe),
86 m3 = m[0].match(ifRe),
87 m4 = m[0].match(execRe),
91 name = m2 && m2[1] ? m2[1] : '';
94 // if - puts fn into test..
95 exp = m3 && m3[1] ? m3[1] : null;
97 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
101 // exec - calls a function... returns empty if true is returned.
102 exp = m4 && m4[1] ? m4[1] : null;
104 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
110 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
111 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
112 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
122 s = s.replace(m[0], '{xtpl'+ id + '}');
125 for(var i = tpls.length-1; i >= 0; --i){
126 this.compileTpl(tpls[i]);
128 this.master = tpls[tpls.length-1];
133 * same as applyTemplate, except it's done to one of the subTemplates
134 * @param {Number} id of the template
135 * @param {Object} values to apply to template
136 * @param {Object} parent (normaly the instance of this object)
138 applySubTemplate : function(id, values, parent)
140 var t = this.tpls[id];
142 if(t.test && !t.test.call(this, values, parent)){
146 Roo.log("Xtemplate.applySubTemplate 'test': Exception thrown");
147 Roo.log(e.toString());
153 if(t.exec && t.exec.call(this, values, parent)){
157 Roo.log("Xtemplate.applySubTemplate 'exec': Exception thrown");
158 Roo.log(e.toString());
163 var vs = t.target ? t.target.call(this, values, parent) : values;
164 parent = t.target ? values : parent;
165 if(t.target && vs instanceof Array){
167 for(var i = 0, len = vs.length; i < len; i++){
168 buf[buf.length] = t.compiled.call(this, vs[i], parent);
172 return t.compiled.call(this, vs, parent);
174 Roo.log("Xtemplate.applySubTemplate : Exception thrown");
175 Roo.log(e.toString());
181 compileTpl : function(tpl)
183 var fm = Roo.util.Format;
184 var useF = this.disableFormats !== true;
185 var sep = Roo.isGecko ? "+" : ",";
186 var undef = function(str) {
187 Roo.log("Property not found :" + str);
191 var fn = function(m, name, format, args)
193 //Roo.log(arguments);
194 args = args ? args.replace(/\\'/g,"'") : args;
195 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
196 if (typeof(format) == 'undefined') {
197 format= 'htmlEncode';
199 if (format == 'raw' ) {
203 if(name.substr(0, 4) == 'xtpl'){
204 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
207 // build an array of options to determine if value is undefined..
209 // basically get 'xxxx.yyyy' then do
210 // (typeof(xxxx) == 'undefined' || typeof(xxx.yyyy) == 'undefined') ?
211 // (function () { Roo.log("Property not found"); return ''; })() :
216 Roo.each(name.split('.'), function(st) {
217 lookfor += (lookfor.length ? '.': '') + st;
218 udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
221 var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; // .. needs )
226 args = args ? ',' + args : "";
228 if(format.substr(0, 5) != "this."){
229 format = "fm." + format + '(';
231 format = 'this.call("'+ format.substr(5) + '", ';
235 return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
239 // called with xxyx.yuu:(test,test)
241 return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
243 // raw.. - :raw modifier..
244 return "'"+ sep + udef_st + name + ")"+sep+"'";
248 // branched to use + in gecko and [].join() in others
250 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
251 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
254 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
255 body.push(tpl.body.replace(/(\r\n|\n)/g,
256 '\\n').replace(/'/g, "\\'").replace(this.re, fn));
257 body.push("'].join('');};};");
258 body = body.join('');
261 Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
263 /** eval:var:zzzzzzz */
269 applyTemplate : function(values){
270 return this.master.compiled.call(this, values, {});
275 return this.applyTemplate.apply(this, arguments);
280 Roo.XTemplate.from = function(el){
282 return new Roo.XTemplate(el.value || el.innerHTML);