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, {
52 * The various sub templates
57 * basic tag replacing syntax
60 * // you can fake an object call by doing this
64 re : /\{([\w-\.]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
67 iterChild : function (node, method) {
68 for( var i = 0; i < node.childNodes.length; i++) {
69 method.call(this, node.childNodes[i]);
74 * compile the template
76 * This is not recursive, so I'm not sure how nested templates are really going to be handled..
83 // covert the html into DOM...
85 var div = document.createElement('div');
86 div.innerHTML = this.html ;
90 this.iterChild(div, function(n) {_t.compileNode(n, true); });
94 // create a top level template from the snippet..
96 Roo.log(div.innerHTML);
103 body : div.innerHTML,
116 Roo.each(tpls, function(tp){
118 this.tpls[tp.id] = tp;
121 this.master = tpls[0];
127 compileNode : function(node, istop) {
131 // skip anything not a tag..
132 if (node.nodeType != 1) {
151 case (node.hasAttribute('roo-for')): tpl.attr = 'for'; break;
152 case (node.hasAttribute('roo-if')): tpl.attr = 'if'; break;
153 case (node.hasAttribute('roo-name')): tpl.attr = 'name'; break;
154 case (node.hasAttribute('roo-exec')): tpl.attr = 'exec'; break;
158 // just itterate children..
159 this.iterChild(node,this.compileNode)
163 var value = node.getAttribute('roo-' + tpl.attr);
164 node.removeAttribute('roo-'+ tpl.attr);
165 if (tpl.attr != 'name') {
166 var placeholder = document.createTextNode('{domtpl' + tpl.uid + '}');
167 node.parentNode.replaceChild(placeholder, node);
169 node.parentNode.removeChild(node);
172 // parent now sees '{domtplXXXX}
173 this.iterChild(node,this.compileNode)
175 // we should now have node body...
176 var div = document.createElement('div');
177 div.appendChild(node);
179 tpl.body = div.innerHTML;
187 case '.': tpl.forCall = new Function('values', 'parent', 'with(values){ return values; }'); break;
188 case '..': tpl.forCall= new Function('values', 'parent', 'with(values){ return parent; }'); break;
189 default: tpl.forCall= new Function('values', 'parent', 'with(values){ return '+tpl.value+'; }');
194 tpl.execCall = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(tpl.value))+'; }');
198 tpl.ifCall = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(tpl.value))+'; }');
202 tpl.id = value; // replace non characters???
215 * same as applyTemplate, except it's done to one of the subTemplates
216 * when using named templates, you can do:
218 * var str = pl.applySubTemplate('your-name', values);
221 * @param {Number} id of the template
222 * @param {Object} values to apply to template
223 * @param {Object} parent (normaly the instance of this object)
225 applySubTemplate : function(id, values, parent)
229 var t = this.tpls[id];
233 if(t.ifCall && !t.ifCall.call(this, values, parent)){
237 Roo.log("Xtemplate.applySubTemplate 'test': Exception thrown");
238 Roo.log(e.toString());
244 if(t.execCall && t.execCall.call(this, values, parent)){
248 Roo.log("Xtemplate.applySubTemplate 'exec': Exception thrown");
249 Roo.log(e.toString());
254 var vs = t.forCall ? t.forCall.call(this, values, parent) : values;
255 parent = t.target ? values : parent;
256 if(t.forCall && vs instanceof Array){
258 for(var i = 0, len = vs.length; i < len; i++){
259 buf[buf.length] = t.compiled.call(this, vs[i], parent);
263 return t.compiled.call(this, vs, parent);
265 Roo.log("Xtemplate.applySubTemplate : Exception thrown");
266 Roo.log(e.toString());
272 compileTpl : function(tpl)
274 var fm = Roo.util.Format;
275 var useF = this.disableFormats !== true;
276 var sep = Roo.isGecko ? "+\n" : ",\n";
277 var undef = function(str) {
278 Roo.log("Property not found :" + str);
282 var fn = function(m, name, format, args)
284 //Roo.log(arguments);
285 args = args ? args.replace(/\\'/g,"'") : args;
286 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
287 if (typeof(format) == 'undefined') {
288 format= 'htmlEncode';
290 if (format == 'raw' ) {
294 if(name.substr(0, 6) == 'domtpl'){
295 return "'"+ sep +'this.applySubTemplate('+name.substr(6)+', values, parent)'+sep+"'";
298 // build an array of options to determine if value is undefined..
300 // basically get 'xxxx.yyyy' then do
301 // (typeof(xxxx) == 'undefined' || typeof(xxx.yyyy) == 'undefined') ?
302 // (function () { Roo.log("Property not found"); return ''; })() :
307 Roo.each(name.split('.'), function(st) {
308 lookfor += (lookfor.length ? '.': '') + st;
309 udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
312 var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; // .. needs )
317 args = args ? ',' + args : "";
319 if(format.substr(0, 5) != "this."){
320 format = "fm." + format + '(';
322 format = 'this.call("'+ format.substr(5) + '", ';
326 return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
330 // called with xxyx.yuu:(test,test)
332 return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
334 // raw.. - :raw modifier..
335 return "'"+ sep + udef_st + name + ")"+sep+"'";
339 // branched to use + in gecko and [].join() in others
341 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
342 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
345 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
346 body.push(tpl.body.replace(/(\r\n|\n)/g,
347 '\\n').replace(/'/g, "\\'").replace(this.re, fn));
348 body.push("'].join('');};};");
349 body = body.join('');
352 Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
354 /** eval:var:tpl eval:var:fm eval:var:useF eval:var:undef */
360 applyTemplate : function(values){
361 return this.master.compiled.call(this, values, {});
366 return this.applyTemplate.apply(this, arguments);
371 Roo.XTemplate.from = function(el){
373 return new Roo.XTemplate(el.value || el.innerHTML);