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) {
133 if (node.nodeType == 3 && !this.inPre) {
135 node.nodeValue = Roo.util.Format.trim(node.value);
156 case (node.hasAttribute('roo-for')): tpl.attr = 'for'; break;
157 case (node.hasAttribute('roo-if')): tpl.attr = 'if'; break;
158 case (node.hasAttribute('roo-name')): tpl.attr = 'name'; break;
159 case (node.hasAttribute('roo-exec')): tpl.attr = 'exec'; break;
163 // just itterate children..
164 this.iterChild(node,this.compileNode)
168 var value = node.getAttribute('roo-' + tpl.attr);
169 node.removeAttribute('roo-'+ tpl.attr);
170 if (tpl.attr != 'name') {
171 var placeholder = document.createTextNode('{domtpl' + tpl.uid + '}');
172 node.parentNode.replaceChild(placeholder, node);
174 node.parentNode.removeChild(node);
177 // parent now sees '{domtplXXXX}
178 this.iterChild(node,this.compileNode)
180 // we should now have node body...
181 var div = document.createElement('div');
182 div.appendChild(node);
184 tpl.body = div.innerHTML;
192 case '.': tpl.forCall = new Function('values', 'parent', 'with(values){ return values; }'); break;
193 case '..': tpl.forCall= new Function('values', 'parent', 'with(values){ return parent; }'); break;
194 default: tpl.forCall= new Function('values', 'parent', 'with(values){ return '+tpl.value+'; }');
199 tpl.execCall = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(tpl.value))+'; }');
203 tpl.ifCall = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(tpl.value))+'; }');
207 tpl.id = value; // replace non characters???
220 * same as applyTemplate, except it's done to one of the subTemplates
221 * when using named templates, you can do:
223 * var str = pl.applySubTemplate('your-name', values);
226 * @param {Number} id of the template
227 * @param {Object} values to apply to template
228 * @param {Object} parent (normaly the instance of this object)
230 applySubTemplate : function(id, values, parent)
234 var t = this.tpls[id];
238 if(t.ifCall && !t.ifCall.call(this, values, parent)){
242 Roo.log("Xtemplate.applySubTemplate 'test': Exception thrown");
243 Roo.log(e.toString());
249 if(t.execCall && t.execCall.call(this, values, parent)){
253 Roo.log("Xtemplate.applySubTemplate 'exec': Exception thrown");
254 Roo.log(e.toString());
259 var vs = t.forCall ? t.forCall.call(this, values, parent) : values;
260 parent = t.target ? values : parent;
261 if(t.forCall && vs instanceof Array){
263 for(var i = 0, len = vs.length; i < len; i++){
264 buf[buf.length] = t.compiled.call(this, vs[i], parent);
268 return t.compiled.call(this, vs, parent);
270 Roo.log("Xtemplate.applySubTemplate : Exception thrown");
271 Roo.log(e.toString());
277 compileTpl : function(tpl)
279 var fm = Roo.util.Format;
280 var useF = this.disableFormats !== true;
281 var sep = Roo.isGecko ? "+\n" : ",\n";
282 var undef = function(str) {
283 Roo.log("Property not found :" + str);
287 var fn = function(m, name, format, args)
289 //Roo.log(arguments);
290 args = args ? args.replace(/\\'/g,"'") : args;
291 //["{TEST:(a,b,c)}", "TEST", "", "a,b,c", 0, "{TEST:(a,b,c)}"]
292 if (typeof(format) == 'undefined') {
293 format= 'htmlEncode';
295 if (format == 'raw' ) {
299 if(name.substr(0, 6) == 'domtpl'){
300 return "'"+ sep +'this.applySubTemplate('+name.substr(6)+', values, parent)'+sep+"'";
303 // build an array of options to determine if value is undefined..
305 // basically get 'xxxx.yyyy' then do
306 // (typeof(xxxx) == 'undefined' || typeof(xxx.yyyy) == 'undefined') ?
307 // (function () { Roo.log("Property not found"); return ''; })() :
312 Roo.each(name.split('.'), function(st) {
313 lookfor += (lookfor.length ? '.': '') + st;
314 udef_ar.push( "(typeof(" + lookfor + ") == 'undefined')" );
317 var udef_st = '((' + udef_ar.join(" || ") +") ? undef('" + name + "') : "; // .. needs )
322 args = args ? ',' + args : "";
324 if(format.substr(0, 5) != "this."){
325 format = "fm." + format + '(';
327 format = 'this.call("'+ format.substr(5) + '", ';
331 return "'"+ sep + udef_st + format + name + args + "))"+sep+"'";
335 // called with xxyx.yuu:(test,test)
337 return "'"+ sep + udef_st + name + '(' + args + "))"+sep+"'";
339 // raw.. - :raw modifier..
340 return "'"+ sep + udef_st + name + ")"+sep+"'";
344 // branched to use + in gecko and [].join() in others
346 body = "tpl.compiled = function(values, parent){ with(values) { return '" +
347 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
350 body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
351 body.push(tpl.body.replace(/(\r\n|\n)/g,
352 '\\n').replace(/'/g, "\\'").replace(this.re, fn));
353 body.push("'].join('');};};");
354 body = body.join('');
357 Roo.debug && Roo.log(body.replace(/\\n/,'\n'));
359 /** eval:var:tpl eval:var:fm eval:var:useF eval:var:undef */
365 applyTemplate : function(values){
366 return this.master.compiled.call(this, values, {});
371 return this.applyTemplate.apply(this, arguments);
376 Roo.XTemplate.from = function(el){
378 return new Roo.XTemplate(el.value || el.innerHTML);