10 * @class Roo.DomTemplate
11 * @extends Roo.Template
12 * An effort at a dom based template engine..
14 * Similar to XTemplate, except it uses dom parsing to create the template..
21 {a_variable} - output encoded.
22 {a_variable.format:("Y-m-d")} - call a method on the variable
23 {a_variable:raw} - unencoded output
24 {a_variable:toFixed(1,2)} - Roo.util.Format."toFixed"
25 {a_variable:this.method_on_template(...)} - call a method on the template object.
30 <div roo-for="a_variable or condition.."></tpl>
31 <tpl roo-if="a_variable or condition"></tpl>
32 <tpl roo-exec="some javascript"></tpl>
33 <tpl roo-name="named_template"></tpl>
38 Roo.XTemplate = function()
40 Roo.XTemplate.superclass.constructor.apply(this, arguments);
47 Roo.extend(Roo.XTemplate, Roo.Template, {
50 * The various sub templates
55 * basic tag replacing syntax
58 * // you can fake an object call by doing this
62 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
65 * compile the template
67 * This is not recursive, so I'm not sure how nested templates are really going to be handled..
74 s = ['<tpl>', s, '</tpl>'].join('');
76 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
77 nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
78 ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
79 execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
80 namedRe = /^<tpl\b[^>]*?name="(\w+)"/, // named templates..
85 while(true == !!(m = s.match(re))){
86 var forMatch = m[0].match(nameRe),
87 ifMatch = m[0].match(ifRe),
88 execMatch = m[0].match(execRe),
89 namedMatch = m[0].match(namedRe),
94 name = forMatch && forMatch[1] ? forMatch[1] : '';
97 // if - puts fn into test..
98 exp = ifMatch && ifMatch[1] ? ifMatch[1] : null;
100 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
105 // exec - calls a function... returns empty if true is returned.
106 exp = execMatch && execMatch[1] ? execMatch[1] : null;
108 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
116 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
117 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
118 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
121 var uid = namedMatch ? namedMatch[1] : id;
125 id: namedMatch ? namedMatch[1] : id,
132 s = s.replace(m[0], '');
134 s = s.replace(m[0], '{xtpl'+ id + '}');
139 for(var i = tpls.length-1; i >= 0; --i){
140 this.compileTpl(tpls[i]);
141 this.tpls[tpls[i].id] = tpls[i];
143 this.master = tpls[tpls.length-1];
147 * same as applyTemplate, except it's done to one of the subTemplates
148 * when using named templates, you can do:
150 * var str = pl.applySubTemplate('your-name', values);
153 * @param {Number} id of the template
154 * @param {Object} values to apply to template
155 * @param {Object} parent (normaly the instance of this object)
157 applySubTemplate : function(id, values, parent)
161 var t = this.tpls[id];
165 if(t.test && !t.test.call(this, values, parent)){
169 Roo.log("Xtemplate.applySubTemplate 'test': Exception thrown");
170 Roo.log(e.toString());
176 if(t.exec && t.exec.call(this, values, parent)){
180 Roo.log("Xtemplate.applySubTemplate 'exec': Exception thrown");
181 Roo.log(e.toString());
186 var vs = t.target ? t.target.call(this, values, parent) : values;
187 parent = t.target ? values : parent;
188 if(t.target && vs instanceof Array){
190 for(var i = 0, len = vs.length; i < len; i++){
191 buf[buf.length] = t.compiled.call(this, vs[i], parent);
195 return t.compiled.call(this, vs, parent);
197 Roo.log("Xtemplate.applySubTemplate : Exception thrown");
198 Roo.log(e.toString());
204 compileTpl : function(tpl)
206 var fm = Roo.util.Format;
207 var useF = this.disableFormats !== true;
208 var sep = Roo.isGecko ? "+" : ",";
209 var undef = function(str) {
210 Roo.log("Property not found :" + str);
214 var fn = function(m, name, format, args)
216 //Roo.log(arguments);
217 args = args ? args.replace(/\\'/g,"'") : args;
218 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
219 if (typeof(format) == 'undefined') {
220 format= 'htmlEncode';
222 if (format == 'raw' ) {
226 if(name.substr(0, 4) == 'xtpl'){
227 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
230 // build an array of options to determine if value is undefined..
232 // basically get 'xxxx.yyyy' then do
233 // (typeof(xxxx) == 'undefined' || typeof(xxx.yyyy) == 'undefined') ?
234 // (function () { Roo.log("Property not found"); return ''; })() :
239 Roo.each(name.split('.'), function(st) {
240 lookfor += (lookfor.length ? '.': '') + st;
241 udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
244 var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; // .. needs )
249 args = args ? ',' + args : "";
251 if(format.substr(0, 5) != "this."){
252 format = "fm." + format + '(';
254 format = 'this.call("'+ format.substr(5) + '", ';
258 return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
262 // called with xxyx.yuu:(test,test)
264 return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
266 // raw.. - :raw modifier..
267 return "'"+ sep + udef_st + name + ")"+sep+"'";
271 // branched to use + in gecko and [].join() in others
273 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
274 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
277 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
278 body.push(tpl.body.replace(/(\r\n|\n)/g,
279 '\\n').replace(/'/g, "\\'").replace(this.re, fn));
280 body.push("'].join('');};};");
281 body = body.join('');
284 Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
286 /** eval:var:tpl eval:var:fm eval:var:useF eval:var:undef */
292 applyTemplate : function(values){
293 return this.master.compiled.call(this, values, {});
298 return this.applyTemplate.apply(this, arguments);
303 Roo.XTemplate.from = function(el){
305 return new Roo.XTemplate(el.value || el.innerHTML);