4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
16 * @class Roo.MasterTemplate
17 * @extends Roo.Template
18 * Provides a template that can have child templates. The syntax is:
20 var t = new Roo.MasterTemplate(
21 '<select name="{name}">',
22 '<tpl name="options"><option value="{value:trim}">{text:ellipsis(10)}</option></tpl>',
25 t.add('options', {value: 'foo', text: 'bar'});
26 // or you can add multiple child elements in one shot
28 {value: 'foo', text: 'bar'},
29 {value: 'foo2', text: 'bar2'},
30 {value: 'foo3', text: 'bar3'}
32 // then append, applying the master template values
33 t.append('my-form', {name: 'my-select'});
35 * A name attribute for the child template is not required if you have only one child
36 * template or you want to refer to them by index.
38 Roo.MasterTemplate = function(){
39 Roo.MasterTemplate.superclass.constructor.apply(this, arguments);
40 this.originalHtml = this.html;
42 var m, re = this.subTemplateRe;
45 while(m = re.exec(this.html)){
46 var name = m[1], content = m[2];
51 tpl : new Roo.Template(content)
54 st[name] = st[subIndex];
56 st[subIndex].tpl.compile();
57 st[subIndex].tpl.call = this.call.createDelegate(this);
60 this.subCount = subIndex;
63 Roo.extend(Roo.MasterTemplate, Roo.Template, {
65 * The regular expression used to match sub templates
69 subTemplateRe : /<tpl(?:\sname="([\w-]+)")?>((?:.|\n)*?)<\/tpl>/gi,
72 * Applies the passed values to a child template.
73 * @param {String/Number} name (optional) The name or index of the child template
74 * @param {Array/Object} values The values to be applied to the template
75 * @return {MasterTemplate} this
77 add : function(name, values){
78 if(arguments.length == 1){
79 values = arguments[0];
82 var s = this.subs[name];
83 s.buffer[s.buffer.length] = s.tpl.apply(values);
88 * Applies all the passed values to a child template.
89 * @param {String/Number} name (optional) The name or index of the child template
90 * @param {Array} values The values to be applied to the template, this should be an array of objects.
91 * @param {Boolean} reset (optional) True to reset the template first
92 * @return {MasterTemplate} this
94 fill : function(name, values, reset){
96 if(a.length == 1 || (a.length == 2 && typeof a[1] == "boolean")){
104 for(var i = 0, len = values.length; i < len; i++){
105 this.add(name, values[i]);
111 * Resets the template for reuse
112 * @return {MasterTemplate} this
116 for(var i = 0; i < this.subCount; i++){
122 applyTemplate : function(values){
124 var replaceIndex = -1;
125 this.html = this.originalHtml.replace(this.subTemplateRe, function(m, name){
126 return s[++replaceIndex].buffer.join("");
128 return Roo.MasterTemplate.superclass.applyTemplate.call(this, values);
132 return this.applyTemplate.apply(this, arguments);
135 compile : function(){return this;}
142 Roo.MasterTemplate.prototype.addAll = Roo.MasterTemplate.prototype.fill;
144 * Creates a template from the passed element's value (display:none textarea, preferred) or innerHTML. e.g.
145 * var tpl = Roo.MasterTemplate.from('element-id');
146 * @param {String/HTMLElement} el
147 * @param {Object} config
150 Roo.MasterTemplate.from = function(el, config){
152 return new Roo.MasterTemplate(el.value || el.innerHTML, config || '');