3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.Template"></div>/**
11 * <p>Represents an HTML fragment template. Templates may be {@link #compile precompiled}
12 * for greater performance.</p>
13 * <p>For example usage {@link #Template see the constructor}.</p>
16 * An instance of this class may be created by passing to the constructor either
17 * a single argument, or multiple arguments:
18 * <div class="mdetail-params"><ul>
19 * <li><b>single argument</b> : String/Array
20 * <div class="sub-desc">
21 * The single argument may be either a String or an Array:<ul>
22 * <li><tt>String</tt> : </li><pre><code>
23 var t = new Ext.Template("<div>Hello {0}.</div>");
24 t.{@link #append}('some-element', ['foo']);
26 * <li><tt>Array</tt> : </li>
27 * An Array will be combined with <code>join('')</code>.
29 var t = new Ext.Template([
30 '<div name="{id}">',
31 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
35 t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
38 * <li><b>multiple arguments</b> : String, Object, Array, ...
39 * <div class="sub-desc">
40 * Multiple arguments will be combined with <code>join('')</code>.
42 var t = new Ext.Template(
43 '<div name="{id}">',
44 '<span class="{cls}">{name} {value}</span>',
46 // a configuration object:
48 compiled: true, // {@link #compile} immediately
49 disableFormats: true // See Notes below.
53 * <p><b>Notes</b>:</p>
54 * <div class="mdetail-params"><ul>
55 * <li>Formatting and <code>disableFormats</code> are not applicable for Ext Core.</li>
56 * <li>For a list of available format functions, see {@link Ext.util.Format}.</li>
57 * <li><code>disableFormats</code> reduces <code>{@link #apply}</code> time
58 * when no formatting is required.</li>
62 * @param {Mixed} config
64 Ext.Template = function(html){
69 if (Ext.isArray(html)) {
71 } else if (a.length > 1) {
72 Ext.each(a, function(v) {
73 if (Ext.isObject(v)) {
84 <div id="cfg-Ext.Template-compiled"></div>/**
85 * @cfg {Boolean} compiled Specify <tt>true</tt> to compile the template
86 * immediately (see <code>{@link #compile}</code>).
87 * Defaults to <tt>false</tt>.
93 Ext.Template.prototype = {
94 <div id="cfg-Ext.Template-re"></div>/**
95 * @cfg {RegExp} re The regular expression used to match template variables.
96 * Defaults to:<pre><code>
97 * re : /\{([\w-]+)\}/g // for Ext Core
98 * re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g // for Ext JS
101 re : /\{([\w-]+)\}/g,
102 <div id="prop-Ext.Template-re"></div>/**
103 * See <code>{@link #re}</code>.
108 <div id="method-Ext.Template-applyTemplate"></div>/**
109 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
110 * @param {Object/Array} values
111 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
112 * or an object (i.e. <code>{foo: 'bar'}</code>).
113 * @return {String} The HTML fragment
115 applyTemplate : function(values){
119 me.compiled(values) :
120 me.html.replace(me.re, function(m, name){
121 return values[name] !== undefined ? values[name] : "";
125 <div id="method-Ext.Template-set"></div>/**
126 * Sets the HTML used as the template and optionally compiles it.
127 * @param {String} html
128 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
129 * @return {Ext.Template} this
131 set : function(html, compile){
135 return compile ? me.compile() : me;
138 <div id="method-Ext.Template-compile"></div>/**
139 * Compiles the template into an internal function, eliminating the RegEx overhead.
140 * @return {Ext.Template} this
142 compile : function(){
144 sep = Ext.isGecko ? "+" : ",";
146 function fn(m, name){
147 name = "values['" + name + "']";
148 return "'"+ sep + '(' + name + " == undefined ? '' : " + name + ')' + sep + "'";
151 eval("this.compiled = function(values){ return " + (Ext.isGecko ? "'" : "['") +
152 me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
153 (Ext.isGecko ? "';};" : "'].join('');};"));
157 <div id="method-Ext.Template-insertFirst"></div>/**
158 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
159 * @param {Mixed} el The context element
160 * @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'})
161 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
162 * @return {HTMLElement/Ext.Element} The new node or Element
164 insertFirst: function(el, values, returnElement){
165 return this.doInsert('afterBegin', el, values, returnElement);
168 <div id="method-Ext.Template-insertBefore"></div>/**
169 * Applies the supplied values to the template and inserts the new node(s) before el.
170 * @param {Mixed} el The context element
171 * @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'})
172 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
173 * @return {HTMLElement/Ext.Element} The new node or Element
175 insertBefore: function(el, values, returnElement){
176 return this.doInsert('beforeBegin', el, values, returnElement);
179 <div id="method-Ext.Template-insertAfter"></div>/**
180 * Applies the supplied values to the template and inserts the new node(s) after el.
181 * @param {Mixed} el The context element
182 * @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'})
183 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
184 * @return {HTMLElement/Ext.Element} The new node or Element
186 insertAfter : function(el, values, returnElement){
187 return this.doInsert('afterEnd', el, values, returnElement);
190 <div id="method-Ext.Template-append"></div>/**
191 * Applies the supplied <code>values</code> to the template and appends
192 * the new node(s) to the specified <code>el</code>.
193 * <p>For example usage {@link #Template see the constructor}.</p>
194 * @param {Mixed} el The context element
195 * @param {Object/Array} values
196 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
197 * or an object (i.e. <code>{foo: 'bar'}</code>).
198 * @param {Boolean} returnElement (optional) true to return an Ext.Element (defaults to undefined)
199 * @return {HTMLElement/Ext.Element} The new node or Element
201 append : function(el, values, returnElement){
202 return this.doInsert('beforeEnd', el, values, returnElement);
205 doInsert : function(where, el, values, returnEl){
207 var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
208 return returnEl ? Ext.get(newNode, true) : newNode;
211 <div id="method-Ext.Template-overwrite"></div>/**
212 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
213 * @param {Mixed} el The context element
214 * @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'})
215 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
216 * @return {HTMLElement/Ext.Element} The new node or Element
218 overwrite : function(el, values, returnElement){
220 el.innerHTML = this.applyTemplate(values);
221 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
224 <div id="method-Ext.Template-apply"></div>/**
225 * Alias for {@link #applyTemplate}
226 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
227 * @param {Object/Array} values
228 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
229 * or an object (i.e. <code>{foo: 'bar'}</code>).
230 * @return {String} The HTML fragment
231 * @member Ext.Template
234 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;
236 <div id="method-Ext.Template-Template.from"></div>/**
237 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
238 * @param {String/HTMLElement} el A DOM element or its id
239 * @param {Object} config A configuration object
240 * @return {Ext.Template} The created template
243 Ext.Template.from = function(el, config){
245 return new Ext.Template(el.value || el.innerHTML, config || '');