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) {
78 for( var i = 0; i < node.childNodes.length; i++) {
79 method.call(this, node.childNodes[i]);
86 * compile the template
88 * This is not recursive, so I'm not sure how nested templates are really going to be handled..
95 // covert the html into DOM...
97 var div = document.createElement('div');
98 div.innerHTML = this.html ;
102 this.iterChild(div, function(n) {_t.compileNode(n, true); });
104 var tpls = this.tpls;
106 // create a top level template from the snippet..
108 Roo.log(div.innerHTML);
115 body : div.innerHTML,
128 Roo.each(tpls, function(tp){
130 this.tpls[tp.id] = tp;
133 this.master = tpls[0];
139 compileNode : function(node, istop) {
143 // skip anything not a tag..
144 if (node.nodeType != 1) {
145 if (node.nodeType == 3 && !this.inPre) {
147 node.nodeValue = Roo.util.Format.trim(node.value);
168 case (node.hasAttribute('roo-for')): tpl.attr = 'for'; break;
169 case (node.hasAttribute('roo-if')): tpl.attr = 'if'; break;
170 case (node.hasAttribute('roo-name')): tpl.attr = 'name'; break;
171 case (node.hasAttribute('roo-exec')): tpl.attr = 'exec'; break;
175 // just itterate children..
176 this.iterChild(node,this.compileNode)
180 var value = node.getAttribute('roo-' + tpl.attr);
181 node.removeAttribute('roo-'+ tpl.attr);
182 if (tpl.attr != 'name') {
183 var placeholder = document.createTextNode('{domtpl' + tpl.uid + '}');
184 node.parentNode.replaceChild(placeholder, node);
186 node.parentNode.removeChild(node);
189 // parent now sees '{domtplXXXX}
190 this.iterChild(node,this.compileNode)
192 // we should now have node body...
193 var div = document.createElement('div');
194 div.appendChild(node);
196 tpl.body = div.innerHTML;
204 case '.': tpl.forCall = new Function('values', 'parent', 'with(values){ return values; }'); break;
205 case '..': tpl.forCall= new Function('values', 'parent', 'with(values){ return parent; }'); break;
206 default: tpl.forCall= new Function('values', 'parent', 'with(values){ return '+tpl.value+'; }');
211 tpl.execCall = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(tpl.value))+'; }');
215 tpl.ifCall = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(tpl.value))+'; }');
219 tpl.id = value; // replace non characters???
232 * same as applyTemplate, except it's done to one of the subTemplates
233 * when using named templates, you can do:
235 * var str = pl.applySubTemplate('your-name', values);
238 * @param {Number} id of the template
239 * @param {Object} values to apply to template
240 * @param {Object} parent (normaly the instance of this object)
242 applySubTemplate : function(id, values, parent)
246 var t = this.tpls[id];
250 if(t.ifCall && !t.ifCall.call(this, values, parent)){
254 Roo.log("Xtemplate.applySubTemplate 'test': Exception thrown");
255 Roo.log(e.toString());
261 if(t.execCall && t.execCall.call(this, values, parent)){
265 Roo.log("Xtemplate.applySubTemplate 'exec': Exception thrown");
266 Roo.log(e.toString());
271 var vs = t.forCall ? t.forCall.call(this, values, parent) : values;
272 parent = t.target ? values : parent;
273 if(t.forCall && vs instanceof Array){
275 for(var i = 0, len = vs.length; i < len; i++){
276 buf[buf.length] = t.compiled.call(this, vs[i], parent);
280 return t.compiled.call(this, vs, parent);
282 Roo.log("Xtemplate.applySubTemplate : Exception thrown");
283 Roo.log(e.toString());
289 compileTpl : function(tpl)
291 var fm = Roo.util.Format;
292 var useF = this.disableFormats !== true;
293 var sep = Roo.isGecko ? "+\n" : ",\n";
294 var undef = function(str) {
295 Roo.log("Property not found :" + str);
299 var fn = function(m, name, format, args)
301 //Roo.log(arguments);
302 args = args ? args.replace(/\\'/g,"'") : args;
303 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
304 if (typeof(format) == 'undefined') {
305 format= 'htmlEncode';
307 if (format == 'raw' ) {
311 if(name.substr(0, 6) == 'domtpl'){
312 return "'"+ sep +'this.applySubTemplate('+name.substr(6)+', values, parent)'+sep+"'";
315 // build an array of options to determine if value is undefined..
317 // basically get 'xxxx.yyyy' then do
318 // (typeof(xxxx) == 'undefined' || typeof(xxx.yyyy) == 'undefined') ?
319 // (function () { Roo.log("Property not found"); return ''; })() :
324 Roo.each(name.split('.'), function(st) {
325 lookfor += (lookfor.length ? '.': '') + st;
326 udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
329 var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; // .. needs )
334 args = args ? ',' + args : "";
336 if(format.substr(0, 5) != "this."){
337 format = "fm." + format + '(';
339 format = 'this.call("'+ format.substr(5) + '", ';
343 return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
347 // called with xxyx.yuu:(test,test)
349 return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
351 // raw.. - :raw modifier..
352 return "'"+ sep + udef_st + name + ")"+sep+"'";
356 // branched to use + in gecko and [].join() in others
358 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
359 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
362 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
363 body.push(tpl.body.replace(/(\r\n|\n)/g,
364 '\\n').replace(/'/g, "\\'").replace(this.re, fn));
365 body.push("'].join('');};};");
366 body = body.join('');
369 Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
371 /** eval:var:tpl eval:var:fm eval:var:useF eval:var:undef */
377 applyTemplate : function(values){
378 return this.master.compiled.call(this, values, {});
383 return this.applyTemplate.apply(this, arguments);
388 Roo.XTemplate.from = function(el){
390 return new Roo.XTemplate(el.value || el.innerHTML);