3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
9 * Represents an HTML fragment template. Templates can be precompiled for greater performance.
10 * For a list of available format functions, see {@link Ext.util.Format}.<br />
13 var t = new Ext.Template(
14 '<div name="{id}">',
15 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
18 t.append('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
21 * @param {String/Array} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
23 Ext.Template = function(html){
28 if (Ext.isArray(html)) {
30 } else if (a.length > 1) {
31 Ext.each(a, function(v) {
32 if (Ext.isObject(v)) {
47 Ext.Template.prototype = {
49 * Returns an HTML fragment of this template with the specified values applied.
50 * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
51 * @return {String} The HTML fragment
53 applyTemplate : function(values){
58 me.html.replace(me.re, function(m, name){
59 return values[name] !== undefined ? values[name] : "";
64 * Sets the HTML used as the template and optionally compiles it.
65 * @param {String} html
66 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
67 * @return {Ext.Template} this
69 set : function(html, compile){
73 return compile ? me.compile() : me;
77 * The regular expression used to match template variables
84 * Compiles the template into an internal function, eliminating the RegEx overhead.
85 * @return {Ext.Template} this
89 sep = Ext.isGecko ? "+" : ",";
92 name = "values['" + name + "']";
93 return "'"+ sep + '(' + name + " == undefined ? '' : " + name + ')' + sep + "'";
96 eval("this.compiled = function(values){ return " + (Ext.isGecko ? "'" : "['") +
97 me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
98 (Ext.isGecko ? "';};" : "'].join('');};"));
103 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
104 * @param {Mixed} el The context element
105 * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
106 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
107 * @return {HTMLElement/Ext.Element} The new node or Element
109 insertFirst: function(el, values, returnElement){
110 return this.doInsert('afterBegin', el, values, returnElement);
114 * Applies the supplied values to the template and inserts the new node(s) before el.
115 * @param {Mixed} el The context element
116 * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
117 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
118 * @return {HTMLElement/Ext.Element} The new node or Element
120 insertBefore: function(el, values, returnElement){
121 return this.doInsert('beforeBegin', el, values, returnElement);
125 * Applies the supplied values to the template and inserts the new node(s) after el.
126 * @param {Mixed} el The context element
127 * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
128 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
129 * @return {HTMLElement/Ext.Element} The new node or Element
131 insertAfter : function(el, values, returnElement){
132 return this.doInsert('afterEnd', el, values, returnElement);
136 * Applies the supplied values to the template and appends the new node(s) to el.
137 * @param {Mixed} el The context element
138 * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
139 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
140 * @return {HTMLElement/Ext.Element} The new node or Element
142 append : function(el, values, returnElement){
143 return this.doInsert('beforeEnd', el, values, returnElement);
146 doInsert : function(where, el, values, returnEl){
148 var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
149 return returnEl ? Ext.get(newNode, true) : newNode;
153 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
154 * @param {Mixed} el The context element
155 * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
156 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
157 * @return {HTMLElement/Ext.Element} The new node or Element
159 overwrite : function(el, values, returnElement){
161 el.innerHTML = this.applyTemplate(values);
162 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
166 * Alias for {@link #applyTemplate}
167 * Returns an HTML fragment of this template with the specified values applied.
168 * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
169 * @return {String} The HTML fragment
170 * @member Ext.Template
173 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;
176 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
177 * @param {String/HTMLElement} el A DOM element or its id
178 * @param {Object} config A configuration object
179 * @return {Ext.Template} The created template
182 Ext.Template.from = function(el, config){
184 return new Ext.Template(el.value || el.innerHTML, config || '');