4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-Template'>/**
19 </span> * @class Ext.Template
20 * <p>Represents an HTML fragment template. Templates may be {@link #compile precompiled}
21 * for greater performance.</p>
22 * An instance of this class may be created by passing to the constructor either
23 * a single argument, or multiple arguments:
24 * <div class="mdetail-params"><ul>
25 * <li><b>single argument</b> : String/Array
26 * <div class="sub-desc">
27 * The single argument may be either a String or an Array:<ul>
28 * <li><tt>String</tt> : </li><pre><code>
29 var t = new Ext.Template("&lt;div>Hello {0}.&lt;/div>");
30 t.{@link #append}('some-element', ['foo']);
31 </code></pre>
32 * <li><tt>Array</tt> : </li>
33 * An Array will be combined with <code>join('')</code>.
34 <pre><code>
35 var t = new Ext.Template([
36 '&lt;div name="{id}"&gt;',
37 '&lt;span class="{cls}"&gt;{name:trim} {value:ellipsis(10)}&lt;/span&gt;',
38 '&lt;/div&gt;',
41 t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
42 </code></pre>
43 * </ul></div></li>
44 * <li><b>multiple arguments</b> : String, Object, Array, ...
45 * <div class="sub-desc">
46 * Multiple arguments will be combined with <code>join('')</code>.
47 * <pre><code>
48 var t = new Ext.Template(
49 '&lt;div name="{id}"&gt;',
50 '&lt;span class="{cls}"&gt;{name} {value}&lt;/span&gt;',
51 '&lt;/div&gt;',
52 // a configuration object:
54 compiled: true, // {@link #compile} immediately
57 </code></pre>
58 * <p><b>Notes</b>:</p>
59 * <div class="mdetail-params"><ul>
60 * <li>For a list of available format functions, see {@link Ext.util.Format}.</li>
61 * <li><code>disableFormats</code> reduces <code>{@link #apply}</code> time
62 * when no formatting is required.</li>
63 * </ul></div>
64 * </div></li>
65 * </ul></div>
66 * @param {Mixed} config
69 Ext.define('Ext.Template', {
71 /* Begin Definitions */
73 requires: ['Ext.core.DomHelper', 'Ext.util.Format'],
76 <span id='Ext-Template-method-from'> /**
77 </span> * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
78 * @param {String/HTMLElement} el A DOM element or its id
79 * @param {Object} config A configuration object
80 * @return {Ext.Template} The created template
83 from: function(el, config) {
85 return new this(el.value || el.innerHTML, config || '');
91 constructor: function(html) {
99 me.initialConfig = {};
102 for (; i < length; i++) {
104 if (typeof value == 'object') {
105 Ext.apply(me.initialConfig, value);
106 Ext.apply(me, value);
111 html = buffer.join('');
113 if (Ext.isArray(html)) {
114 buffer.push(html.join(''));
121 me.html = buffer.join('');
128 <span id='Ext-Template-cfg-disableFormats'> /**
129 </span> * @cfg {Boolean} disableFormats true to disable format functions in the template. If the template doesn't contain format functions, setting
130 * disableFormats to true will reduce apply time (defaults to false)
132 disableFormats: false,
134 re: /\{([\w\-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
135 <span id='Ext-Template-method-applyTemplate'> /**
136 </span> * Returns an HTML fragment of this template with the specified values applied.
137 * @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'})
138 * @return {String} The HTML fragment
140 applyTemplate: function(values) {
142 useFormat = me.disableFormats !== true,
143 fm = Ext.util.Format,
147 return me.compiled(values);
149 function fn(m, name, format, args) {
150 if (format && useFormat) {
152 args = [values[name]].concat(Ext.functionFactory('return ['+ args +'];')());
154 args = [values[name]];
156 if (format.substr(0, 5) == "this.") {
157 return tpl[format.substr(5)].apply(tpl, args);
160 return fm[format].apply(fm, args);
164 return values[name] !== undefined ? values[name] : "";
167 return me.html.replace(me.re, fn);
170 <span id='Ext-Template-method-set'> /**
171 </span> * Sets the HTML used as the template and optionally compiles it.
172 * @param {String} html
173 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
174 * @return {Ext.Template} this
176 set: function(html, compile) {
180 return compile ? me.compile() : me;
184 compileBRe: /(\r\n|\n)/g,
186 <span id='Ext-Template-method-compile'> /**
187 </span> * Compiles the template into an internal function, eliminating the RegEx overhead.
188 * @return {Ext.Template} this
190 compile: function() {
192 fm = Ext.util.Format,
193 useFormat = me.disableFormats !== true,
196 function fn(m, name, format, args) {
197 if (format && useFormat) {
198 args = args ? ',' + args: "";
199 if (format.substr(0, 5) != "this.") {
200 format = "fm." + format + '(';
203 format = 'this.' + format.substr(5) + '(';
208 format = "(values['" + name + "'] == undefined ? '' : ";
210 return "'," + format + "values['" + name + "']" + args + ") ,'";
213 bodyReturn = me.html.replace(me.compileARe, '\\\\').replace(me.compileBRe, '\\n').replace(me.compileCRe, "\\'").replace(me.re, fn);
214 body = "this.compiled = function(values){ return ['" + bodyReturn + "'].join('');};";
219 <span id='Ext-Template-method-insertFirst'> /**
220 </span> * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
221 * @param {Mixed} el The context element
222 * @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'})
223 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
224 * @return {HTMLElement/Ext.core.Element} The new node or Element
226 insertFirst: function(el, values, returnElement) {
227 return this.doInsert('afterBegin', el, values, returnElement);
230 <span id='Ext-Template-method-insertBefore'> /**
231 </span> * Applies the supplied values to the template and inserts the new node(s) before el.
232 * @param {Mixed} el The context element
233 * @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'})
234 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
235 * @return {HTMLElement/Ext.core.Element} The new node or Element
237 insertBefore: function(el, values, returnElement) {
238 return this.doInsert('beforeBegin', el, values, returnElement);
241 <span id='Ext-Template-method-insertAfter'> /**
242 </span> * Applies the supplied values to the template and inserts the new node(s) after el.
243 * @param {Mixed} el The context element
244 * @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'})
245 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
246 * @return {HTMLElement/Ext.core.Element} The new node or Element
248 insertAfter: function(el, values, returnElement) {
249 return this.doInsert('afterEnd', el, values, returnElement);
252 <span id='Ext-Template-method-append'> /**
253 </span> * Applies the supplied <code>values</code> to the template and appends
254 * the new node(s) to the specified <code>el</code>.
255 * <p>For example usage {@link #Template see the constructor}.</p>
256 * @param {Mixed} el The context element
257 * @param {Object/Array} values
258 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
259 * or an object (i.e. <code>{foo: 'bar'}</code>).
260 * @param {Boolean} returnElement (optional) true to return an Ext.core.Element (defaults to undefined)
261 * @return {HTMLElement/Ext.core.Element} The new node or Element
263 append: function(el, values, returnElement) {
264 return this.doInsert('beforeEnd', el, values, returnElement);
267 doInsert: function(where, el, values, returnEl) {
269 var newNode = Ext.core.DomHelper.insertHtml(where, el, this.applyTemplate(values));
270 return returnEl ? Ext.get(newNode, true) : newNode;
273 <span id='Ext-Template-method-overwrite'> /**
274 </span> * Applies the supplied values to the template and overwrites the content of el with the new node(s).
275 * @param {Mixed} el The context element
276 * @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'})
277 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
278 * @return {HTMLElement/Ext.core.Element} The new node or Element
280 overwrite: function(el, values, returnElement) {
282 el.innerHTML = this.applyTemplate(values);
283 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
287 <span id='Ext-Template-method-apply'> /**
288 </span> * Alias for {@link #applyTemplate}
289 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
290 * @param {Object/Array} values
291 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
292 * or an object (i.e. <code>{foo: 'bar'}</code>).
293 * @return {String} The HTML fragment
294 * @member Ext.Template
297 this.createAlias('apply', 'applyTemplate');