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 iterNodes : function (nodeAr, method) {
66 for( var i = 0; i < nodeAr.length; i++) {
67 method.call(this, nodeAr[i]);
75 * compile the template
77 * This is not recursive, so I'm not sure how nested templates are really going to be handled..
84 // covert the html into DOM...
86 var div = document.createElement('div');
87 div.innerHTML = this.html;
94 s = ['<tpl>', s, '</tpl>'].join('');
96 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
97 nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
98 ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
99 execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
100 namedRe = /^<tpl\b[^>]*?name="(\w+)"/, // named templates..
105 while(true == !!(m = s.match(re))){
106 var forMatch = m[0].match(nameRe),
107 ifMatch = m[0].match(ifRe),
108 execMatch = m[0].match(execRe),
109 namedMatch = m[0].match(namedRe),
114 name = forMatch && forMatch[1] ? forMatch[1] : '';
117 // if - puts fn into test..
118 exp = ifMatch && ifMatch[1] ? ifMatch[1] : null;
120 fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
125 // exec - calls a function... returns empty if true is returned.
126 exp = execMatch && execMatch[1] ? execMatch[1] : null;
128 exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
136 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
137 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
138 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
141 var uid = namedMatch ? namedMatch[1] : id;
145 id: namedMatch ? namedMatch[1] : id,
152 s = s.replace(m[0], '');
154 s = s.replace(m[0], '{xtpl'+ id + '}');
159 for(var i = tpls.length-1; i >= 0; --i){
160 this.compileTpl(tpls[i]);
161 this.tpls[tpls[i].id] = tpls[i];
163 this.master = tpls[tpls.length-1];
167 * same as applyTemplate, except it's done to one of the subTemplates
168 * when using named templates, you can do:
170 * var str = pl.applySubTemplate('your-name', values);
173 * @param {Number} id of the template
174 * @param {Object} values to apply to template
175 * @param {Object} parent (normaly the instance of this object)
177 applySubTemplate : function(id, values, parent)
181 var t = this.tpls[id];
185 if(t.test && !t.test.call(this, values, parent)){
189 Roo.log("Xtemplate.applySubTemplate 'test': Exception thrown");
190 Roo.log(e.toString());
196 if(t.exec && t.exec.call(this, values, parent)){
200 Roo.log("Xtemplate.applySubTemplate 'exec': Exception thrown");
201 Roo.log(e.toString());
206 var vs = t.target ? t.target.call(this, values, parent) : values;
207 parent = t.target ? values : parent;
208 if(t.target && vs instanceof Array){
210 for(var i = 0, len = vs.length; i < len; i++){
211 buf[buf.length] = t.compiled.call(this, vs[i], parent);
215 return t.compiled.call(this, vs, parent);
217 Roo.log("Xtemplate.applySubTemplate : Exception thrown");
218 Roo.log(e.toString());
224 compileTpl : function(tpl)
226 var fm = Roo.util.Format;
227 var useF = this.disableFormats !== true;
228 var sep = Roo.isGecko ? "+" : ",";
229 var undef = function(str) {
230 Roo.log("Property not found :" + str);
234 var fn = function(m, name, format, args)
236 //Roo.log(arguments);
237 args = args ? args.replace(/\\'/g,"'") : args;
238 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
239 if (typeof(format) == 'undefined') {
240 format= 'htmlEncode';
242 if (format == 'raw' ) {
246 if(name.substr(0, 4) == 'xtpl'){
247 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
250 // build an array of options to determine if value is undefined..
252 // basically get 'xxxx.yyyy' then do
253 // (typeof(xxxx) == 'undefined' || typeof(xxx.yyyy) == 'undefined') ?
254 // (function () { Roo.log("Property not found"); return ''; })() :
259 Roo.each(name.split('.'), function(st) {
260 lookfor += (lookfor.length ? '.': '') + st;
261 udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
264 var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; // .. needs )
269 args = args ? ',' + args : "";
271 if(format.substr(0, 5) != "this."){
272 format = "fm." + format + '(';
274 format = 'this.call("'+ format.substr(5) + '", ';
278 return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
282 // called with xxyx.yuu:(test,test)
284 return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
286 // raw.. - :raw modifier..
287 return "'"+ sep + udef_st + name + ")"+sep+"'";
291 // branched to use + in gecko and [].join() in others
293 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
294 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
297 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
298 body.push(tpl.body.replace(/(\r\n|\n)/g,
299 '\\n').replace(/'/g, "\\'").replace(this.re, fn));
300 body.push("'].join('');};};");
301 body = body.join('');
304 Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
306 /** eval:var:tpl eval:var:fm eval:var:useF eval:var:undef */
312 applyTemplate : function(values){
313 return this.master.compiled.call(this, values, {});
318 return this.applyTemplate.apply(this, arguments);
323 Roo.XTemplate.from = function(el){
325 return new Roo.XTemplate(el.value || el.innerHTML);