3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
9 * <p>Represents an HTML fragment template. Templates may be {@link #compile precompiled}
10 * for greater performance.</p>
11 * <p>For example usage {@link #Template see the constructor}.</p>
14 * An instance of this class may be created by passing to the constructor either
15 * a single argument, or multiple arguments:
16 * <div class="mdetail-params"><ul>
17 * <li><b>single argument</b> : String/Array
18 * <div class="sub-desc">
19 * The single argument may be either a String or an Array:<ul>
20 * <li><tt>String</tt> : </li><pre><code>
21 var t = new Ext.Template("<div>Hello {0}.</div>");
22 t.{@link #append}('some-element', ['foo']);
24 * <li><tt>Array</tt> : </li>
25 * An Array will be combined with <code>join('')</code>.
27 var t = new Ext.Template([
28 '<div name="{id}">',
29 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
33 t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
36 * <li><b>multiple arguments</b> : String, Object, Array, ...
37 * <div class="sub-desc">
38 * Multiple arguments will be combined with <code>join('')</code>.
40 var t = new Ext.Template(
41 '<div name="{id}">',
42 '<span class="{cls}">{name} {value}</span>',
44 // a configuration object:
46 compiled: true, // {@link #compile} immediately
47 disableFormats: true // See Notes below.
51 * <p><b>Notes</b>:</p>
52 * <div class="mdetail-params"><ul>
53 * <li>Formatting and <code>disableFormats</code> are not applicable for Ext Core.</li>
54 * <li>For a list of available format functions, see {@link Ext.util.Format}.</li>
55 * <li><code>disableFormats</code> reduces <code>{@link #apply}</code> time
56 * when no formatting is required.</li>
60 * @param {Mixed} config
62 Ext.Template = function(html){
67 if (Ext.isArray(html)) {
69 } else if (a.length > 1) {
70 Ext.each(a, function(v) {
71 if (Ext.isObject(v)) {
83 * @cfg {Boolean} compiled Specify <tt>true</tt> to compile the template
84 * immediately (see <code>{@link #compile}</code>).
85 * Defaults to <tt>false</tt>.
91 Ext.Template.prototype = {
93 * @cfg {RegExp} re The regular expression used to match template variables.
94 * Defaults to:<pre><code>
95 * re : /\{([\w-]+)\}/g // for Ext Core
96 * re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g // for Ext JS
101 * See <code>{@link #re}</code>.
107 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
108 * @param {Object/Array} values
109 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
110 * or an object (i.e. <code>{foo: 'bar'}</code>).
111 * @return {String} The HTML fragment
113 applyTemplate : function(values){
117 me.compiled(values) :
118 me.html.replace(me.re, function(m, name){
119 return values[name] !== undefined ? values[name] : "";
124 * Sets the HTML used as the template and optionally compiles it.
125 * @param {String} html
126 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
127 * @return {Ext.Template} this
129 set : function(html, compile){
133 return compile ? me.compile() : me;
137 * Compiles the template into an internal function, eliminating the RegEx overhead.
138 * @return {Ext.Template} this
140 compile : function(){
142 sep = Ext.isGecko ? "+" : ",";
144 function fn(m, name){
145 name = "values['" + name + "']";
146 return "'"+ sep + '(' + name + " == undefined ? '' : " + name + ')' + sep + "'";
149 eval("this.compiled = function(values){ return " + (Ext.isGecko ? "'" : "['") +
150 me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
151 (Ext.isGecko ? "';};" : "'].join('');};"));
156 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
157 * @param {Mixed} el The context element
158 * @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'})
159 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
160 * @return {HTMLElement/Ext.Element} The new node or Element
162 insertFirst: function(el, values, returnElement){
163 return this.doInsert('afterBegin', el, values, returnElement);
167 * Applies the supplied values to the template and inserts the new node(s) before el.
168 * @param {Mixed} el The context element
169 * @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'})
170 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
171 * @return {HTMLElement/Ext.Element} The new node or Element
173 insertBefore: function(el, values, returnElement){
174 return this.doInsert('beforeBegin', el, values, returnElement);
178 * Applies the supplied values to the template and inserts the new node(s) after el.
179 * @param {Mixed} el The context element
180 * @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'})
181 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
182 * @return {HTMLElement/Ext.Element} The new node or Element
184 insertAfter : function(el, values, returnElement){
185 return this.doInsert('afterEnd', el, values, returnElement);
189 * Applies the supplied <code>values</code> to the template and appends
190 * the new node(s) to the specified <code>el</code>.
191 * <p>For example usage {@link #Template see the constructor}.</p>
192 * @param {Mixed} el The context element
193 * @param {Object/Array} values
194 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
195 * or an object (i.e. <code>{foo: 'bar'}</code>).
196 * @param {Boolean} returnElement (optional) true to return an Ext.Element (defaults to undefined)
197 * @return {HTMLElement/Ext.Element} The new node or Element
199 append : function(el, values, returnElement){
200 return this.doInsert('beforeEnd', el, values, returnElement);
203 doInsert : function(where, el, values, returnEl){
205 var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
206 return returnEl ? Ext.get(newNode, true) : newNode;
210 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
211 * @param {Mixed} el The context element
212 * @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'})
213 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
214 * @return {HTMLElement/Ext.Element} The new node or Element
216 overwrite : function(el, values, returnElement){
218 el.innerHTML = this.applyTemplate(values);
219 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
223 * Alias for {@link #applyTemplate}
224 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
225 * @param {Object/Array} values
226 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
227 * or an object (i.e. <code>{foo: 'bar'}</code>).
228 * @return {String} The HTML fragment
229 * @member Ext.Template
232 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;
235 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
236 * @param {String/HTMLElement} el A DOM element or its id
237 * @param {Object} config A configuration object
238 * @return {Ext.Template} The created template
241 Ext.Template.from = function(el, config){
243 return new Ext.Template(el.value || el.innerHTML, config || '');