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
141 applyTemplate: function(values) {
143 useFormat = me.disableFormats !== true,
144 fm = Ext.util.Format,
148 return me.compiled(values);
150 function fn(m, name, format, args) {
151 if (format && useFormat) {
153 args = [values[name]].concat(Ext.functionFactory('return ['+ args +'];')());
155 args = [values[name]];
157 if (format.substr(0, 5) == "this.") {
158 return tpl[format.substr(5)].apply(tpl, args);
161 return fm[format].apply(fm, args);
165 return values[name] !== undefined ? values[name] : "";
168 return me.html.replace(me.re, fn);
171 <span id='Ext-Template-method-set'> /**
172 </span> * Sets the HTML used as the template and optionally compiles it.
173 * @param {String} html
174 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
175 * @return {Ext.Template} this
177 set: function(html, compile) {
181 return compile ? me.compile() : me;
185 compileBRe: /(\r\n|\n)/g,
187 <span id='Ext-Template-method-compile'> /**
188 </span> * Compiles the template into an internal function, eliminating the RegEx overhead.
189 * @return {Ext.Template} this
192 compile: function() {
194 fm = Ext.util.Format,
195 useFormat = me.disableFormats !== true,
198 function fn(m, name, format, args) {
199 if (format && useFormat) {
200 args = args ? ',' + args: "";
201 if (format.substr(0, 5) != "this.") {
202 format = "fm." + format + '(';
205 format = 'this.' + format.substr(5) + '(';
210 format = "(values['" + name + "'] == undefined ? '' : ";
212 return "'," + format + "values['" + name + "']" + args + ") ,'";
215 bodyReturn = me.html.replace(me.compileARe, '\\\\').replace(me.compileBRe, '\\n').replace(me.compileCRe, "\\'").replace(me.re, fn);
216 body = "this.compiled = function(values){ return ['" + bodyReturn + "'].join('');};";
221 <span id='Ext-Template-method-insertFirst'> /**
222 </span> * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
223 * @param {Mixed} el The context element
224 * @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'})
225 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
226 * @return {HTMLElement/Ext.core.Element} The new node or Element
228 insertFirst: function(el, values, returnElement) {
229 return this.doInsert('afterBegin', el, values, returnElement);
232 <span id='Ext-Template-method-insertBefore'> /**
233 </span> * Applies the supplied values to the template and inserts the new node(s) before el.
234 * @param {Mixed} el The context element
235 * @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'})
236 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
237 * @return {HTMLElement/Ext.core.Element} The new node or Element
239 insertBefore: function(el, values, returnElement) {
240 return this.doInsert('beforeBegin', el, values, returnElement);
243 <span id='Ext-Template-method-insertAfter'> /**
244 </span> * Applies the supplied values to the template and inserts the new node(s) after el.
245 * @param {Mixed} el The context element
246 * @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'})
247 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
248 * @return {HTMLElement/Ext.core.Element} The new node or Element
250 insertAfter: function(el, values, returnElement) {
251 return this.doInsert('afterEnd', el, values, returnElement);
254 <span id='Ext-Template-method-append'> /**
255 </span> * Applies the supplied <code>values</code> to the template and appends
256 * the new node(s) to the specified <code>el</code>.
257 * <p>For example usage {@link #Template see the constructor}.</p>
258 * @param {Mixed} el The context element
259 * @param {Object/Array} values
260 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
261 * or an object (i.e. <code>{foo: 'bar'}</code>).
262 * @param {Boolean} returnElement (optional) true to return an Ext.core.Element (defaults to undefined)
263 * @return {HTMLElement/Ext.core.Element} The new node or Element
265 append: function(el, values, returnElement) {
266 return this.doInsert('beforeEnd', el, values, returnElement);
269 doInsert: function(where, el, values, returnEl) {
271 var newNode = Ext.core.DomHelper.insertHtml(where, el, this.applyTemplate(values));
272 return returnEl ? Ext.get(newNode, true) : newNode;
275 <span id='Ext-Template-method-overwrite'> /**
276 </span> * Applies the supplied values to the template and overwrites the content of el with the new node(s).
277 * @param {Mixed} el The context element
278 * @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'})
279 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
280 * @return {HTMLElement/Ext.core.Element} The new node or Element
282 overwrite: function(el, values, returnElement) {
284 el.innerHTML = this.applyTemplate(values);
285 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
289 <span id='Ext-Template-method-apply'> /**
290 </span> * Alias for {@link #applyTemplate}
291 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
292 * @param {Object/Array} values
293 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
294 * or an object (i.e. <code>{foo: 'bar'}</code>).
295 * @return {String} The HTML fragment
296 * @member Ext.Template
299 this.createAlias('apply', 'applyTemplate');