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.DomTemplate = function()
40 Roo.DomTemplate.superclass.constructor.apply(this, arguments);
47 Roo.extend(Roo.DomTemplate, Roo.Template, {
49 * id counter for sub templates.
53 * flag to indicate if dom parser is inside a pre,
54 * it will strip whitespace if not.
59 * The various sub templates
67 * basic tag replacing syntax
70 * // you can fake an object call by doing this
74 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
77 iterChild : function (node, method) {
79 var oldPre = this.inPre;
80 if (node.tagName == 'PRE') {
83 for( var i = 0; i < node.childNodes.length; i++) {
84 method.call(this, node.childNodes[i]);
92 * compile the template
94 * This is not recursive, so I'm not sure how nested templates are really going to be handled..
101 // covert the html into DOM...
103 var div = document.createElement('div');
104 div.innerHTML = this.html ;
108 this.iterChild(div, function(n) {_t.compileNode(n, true); });
110 var tpls = this.tpls;
112 // create a top level template from the snippet..
114 Roo.log(div.innerHTML);
121 body : div.innerHTML,
134 Roo.each(tpls, function(tp){
136 this.tpls[tp.id] = tp;
139 this.master = tpls[0];
145 compileNode : function(node, istop) {
150 // skip anything not a tag..
151 if (node.nodeType != 1) {
152 if (node.nodeType == 3 && !this.inPre) {
153 // reduce white space..
154 node.nodeValue = node.nodeValue.replace(/\s+/g, ' ');
177 case (node.hasAttribute('roo-for')): tpl.attr = 'for'; break;
178 case (node.hasAttribute('roo-if')): tpl.attr = 'if'; break;
179 case (node.hasAttribute('roo-name')): tpl.attr = 'name'; break;
180 case (node.hasAttribute('roo-exec')): tpl.attr = 'exec'; break;
186 // just itterate children..
187 this.iterChild(node,this.compileNode)
191 tpl.value = node.getAttribute('roo-' + tpl.attr);
192 node.removeAttribute('roo-'+ tpl.attr);
193 if (tpl.attr != 'name') {
194 var placeholder = document.createTextNode('{domtpl' + tpl.uid + '}');
195 node.parentNode.replaceChild(placeholder, node);
198 var placeholder = Roo.DomHelper.document.createElement('span');
199 placeholder.className = 'roo-tpl-' + value;
201 node.parentNode.replaceChild(placeholder, node);
204 // parent now sees '{domtplXXXX}
205 this.iterChild(node,this.compileNode)
207 // we should now have node body...
208 var div = document.createElement('div');
209 div.appendChild(node);
211 tpl.body = div.innerHTML;
219 case '.': tpl.forCall = new Function('values', 'parent', 'with(values){ return values; }'); break;
220 case '..': tpl.forCall= new Function('values', 'parent', 'with(values){ return parent; }'); break;
221 default: tpl.forCall= new Function('values', 'parent', 'with(values){ return '+tpl.value+'; }');
226 tpl.execCall = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(tpl.value))+'; }');
230 tpl.ifCall = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(tpl.value))+'; }');
234 tpl.id = tpl.value; // replace non characters???
246 * Compile a segment of the template into a 'sub-template'
250 compileTpl : function(tpl)
252 var fm = Roo.util.Format;
253 var useF = this.disableFormats !== true;
254 var sep = Roo.isGecko ? "+\n" : ",\n";
255 var undef = function(str) {
256 Roo.log("Property not found :" + str);
260 var fn = function(m, name, format, args)
262 //Roo.log(arguments);
263 args = args ? args.replace(/\\'/g,"'") : args;
264 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
265 if (typeof(format) == 'undefined') {
266 format= 'htmlEncode';
268 if (format == 'raw' ) {
272 if(name.substr(0, 6) == 'domtpl'){
273 return "'"+ sep +'this.applySubTemplate('+name.substr(6)+', values, parent)'+sep+"'";
276 // build an array of options to determine if value is undefined..
278 // basically get 'xxxx.yyyy' then do
279 // (typeof(xxxx) == 'undefined' || typeof(xxx.yyyy) == 'undefined') ?
280 // (function () { Roo.log("Property not found"); return ''; })() :
285 Roo.each(name.split('.'), function(st) {
286 lookfor += (lookfor.length ? '.': '') + st;
287 udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
290 var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; // .. needs )
295 args = args ? ',' + args : "";
297 if(format.substr(0, 5) != "this."){
298 format = "fm." + format + '(';
300 format = 'this.call("'+ format.substr(5) + '", ';
304 return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
308 // called with xxyx.yuu:(test,test)
310 return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
312 // raw.. - :raw modifier..
313 return "'"+ sep + udef_st + name + ")"+sep+"'";
317 // branched to use + in gecko and [].join() in others
319 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
320 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
323 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
324 body.push(tpl.body.replace(/(\r\n|\n)/g,
325 '\\n').replace(/'/g, "\\'").replace(this.re, fn));
326 body.push("'].join('');};};");
327 body = body.join('');
330 Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
332 /** eval:var:tpl eval:var:fm eval:var:useF eval:var:undef */
339 * same as applyTemplate, except it's done to one of the subTemplates
340 * when using named templates, you can do:
342 * var str = pl.applySubTemplate('your-name', values);
345 * @param {Number} id of the template
346 * @param {Object} values to apply to template
347 * @param {Object} parent (normaly the instance of this object)
349 applySubTemplate : function(id, values, parent)
353 var t = this.tpls[id];
357 if(t.ifCall && !t.ifCall.call(this, values, parent)){
358 Roo.log('if call on ' + t.value + ' return false')
362 Roo.log('Xtemplate.applySubTemplate('+ id+ '): Exception thrown on roo-if="' + t.value + '" - ' + e.toString());
369 if(t.execCall && t.execCall.call(this, values, parent)){
373 Roo.log('Xtemplate.applySubTemplate('+ id+ '): Exception thrown on roo-for="' + t.value + '" - ' + e.toString());
379 var vs = t.forCall ? t.forCall.call(this, values, parent) : values;
380 parent = t.target ? values : parent;
381 if(t.forCall && vs instanceof Array){
383 for(var i = 0, len = vs.length; i < len; i++){
385 buf[buf.length] = t.compiled.call(this, vs[i], parent);
387 Roo.log('Xtemplate.applySubTemplate('+ id+ '): Exception thrown on body="' + t.value + '" - ' + e.toString());
389 //Roo.log(t.compiled);
396 Roo.log('Xtemplate.applySubTemplate('+ id+ '): Exception thrown on roo-for="' + t.value + '" - ' + e.toString());
401 return t.compiled.call(this, vs, parent);
403 Roo.log('Xtemplate.applySubTemplate('+ id+ '): Exception thrown on body="' + t.value + '" - ' + e.toString());
405 //Roo.log(t.compiled);
413 applyTemplate : function(values){
414 return this.master.compiled.call(this, values, {});
419 return this.applyTemplate.apply(this, arguments);
424 Roo.XTemplate.from = function(el){
426 return new Roo.XTemplate(el.value || el.innerHTML);