1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-Template'>/**
2 </span> * @class Ext.Template
3 * <p>Represents an HTML fragment template. Templates may be {@link #compile precompiled}
4 * for greater performance.</p>
5 * An instance of this class may be created by passing to the constructor either
6 * a single argument, or multiple arguments:
7 * <div class="mdetail-params"><ul>
8 * <li><b>single argument</b> : String/Array
9 * <div class="sub-desc">
10 * The single argument may be either a String or an Array:<ul>
11 * <li><tt>String</tt> : </li><pre><code>
12 var t = new Ext.Template("&lt;div>Hello {0}.&lt;/div>");
13 t.{@link #append}('some-element', ['foo']);
14 </code></pre>
15 * <li><tt>Array</tt> : </li>
16 * An Array will be combined with <code>join('')</code>.
17 <pre><code>
18 var t = new Ext.Template([
19 '&lt;div name="{id}"&gt;',
20 '&lt;span class="{cls}"&gt;{name:trim} {value:ellipsis(10)}&lt;/span&gt;',
21 '&lt;/div&gt;',
24 t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
25 </code></pre>
26 * </ul></div></li>
27 * <li><b>multiple arguments</b> : String, Object, Array, ...
28 * <div class="sub-desc">
29 * Multiple arguments will be combined with <code>join('')</code>.
30 * <pre><code>
31 var t = new Ext.Template(
32 '&lt;div name="{id}"&gt;',
33 '&lt;span class="{cls}"&gt;{name} {value}&lt;/span&gt;',
34 '&lt;/div&gt;',
35 // a configuration object:
37 compiled: true, // {@link #compile} immediately
40 </code></pre>
41 * <p><b>Notes</b>:</p>
42 * <div class="mdetail-params"><ul>
43 * <li>For a list of available format functions, see {@link Ext.util.Format}.</li>
44 * <li><code>disableFormats</code> reduces <code>{@link #apply}</code> time
45 * when no formatting is required.</li>
46 * </ul></div>
47 * </div></li>
48 * </ul></div>
49 * @param {Mixed} config
52 Ext.define('Ext.Template', {
54 /* Begin Definitions */
56 requires: ['Ext.core.DomHelper', 'Ext.util.Format'],
59 <span id='Ext-Template-method-from'> /**
60 </span> * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
61 * @param {String/HTMLElement} el A DOM element or its id
62 * @param {Object} config A configuration object
63 * @return {Ext.Template} The created template
66 from: function(el, config) {
68 return new this(el.value || el.innerHTML, config || '');
74 constructor: function(html) {
82 me.initialConfig = {};
85 for (; i < length; i++) {
87 if (typeof value == 'object') {
88 Ext.apply(me.initialConfig, value);
94 html = buffer.join('');
96 if (Ext.isArray(html)) {
97 buffer.push(html.join(''));
104 me.html = buffer.join('');
111 <span id='Ext-Template-cfg-disableFormats'> /**
112 </span> * @cfg {Boolean} disableFormats true to disable format functions in the template. If the template doesn't contain format functions, setting
113 * disableFormats to true will reduce apply time (defaults to false)
115 disableFormats: false,
117 re: /\{([\w\-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
118 <span id='Ext-Template-method-applyTemplate'> /**
119 </span> * Returns an HTML fragment of this template with the specified values applied.
120 * @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'})
121 * @return {String} The HTML fragment
124 applyTemplate: function(values) {
126 useFormat = me.disableFormats !== true,
127 fm = Ext.util.Format,
131 return me.compiled(values);
133 function fn(m, name, format, args) {
134 if (format && useFormat) {
136 args = [values[name]].concat(Ext.functionFactory('return ['+ args +'];')());
138 args = [values[name]];
140 if (format.substr(0, 5) == "this.") {
141 return tpl[format.substr(5)].apply(tpl, args);
144 return fm[format].apply(fm, args);
148 return values[name] !== undefined ? values[name] : "";
151 return me.html.replace(me.re, fn);
154 <span id='Ext-Template-method-set'> /**
155 </span> * Sets the HTML used as the template and optionally compiles it.
156 * @param {String} html
157 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
158 * @return {Ext.Template} this
160 set: function(html, compile) {
164 return compile ? me.compile() : me;
168 compileBRe: /(\r\n|\n)/g,
170 <span id='Ext-Template-method-compile'> /**
171 </span> * Compiles the template into an internal function, eliminating the RegEx overhead.
172 * @return {Ext.Template} this
175 compile: function() {
177 fm = Ext.util.Format,
178 useFormat = me.disableFormats !== true,
181 function fn(m, name, format, args) {
182 if (format && useFormat) {
183 args = args ? ',' + args: "";
184 if (format.substr(0, 5) != "this.") {
185 format = "fm." + format + '(';
188 format = 'this.' + format.substr(5) + '(';
193 format = "(values['" + name + "'] == undefined ? '' : ";
195 return "'," + format + "values['" + name + "']" + args + ") ,'";
198 bodyReturn = me.html.replace(me.compileARe, '\\\\').replace(me.compileBRe, '\\n').replace(me.compileCRe, "\\'").replace(me.re, fn);
199 body = "this.compiled = function(values){ return ['" + bodyReturn + "'].join('');};";
204 <span id='Ext-Template-method-insertFirst'> /**
205 </span> * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
206 * @param {Mixed} el The context element
207 * @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'})
208 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
209 * @return {HTMLElement/Ext.core.Element} The new node or Element
211 insertFirst: function(el, values, returnElement) {
212 return this.doInsert('afterBegin', el, values, returnElement);
215 <span id='Ext-Template-method-insertBefore'> /**
216 </span> * Applies the supplied values to the template and inserts the new node(s) before el.
217 * @param {Mixed} el The context element
218 * @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'})
219 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
220 * @return {HTMLElement/Ext.core.Element} The new node or Element
222 insertBefore: function(el, values, returnElement) {
223 return this.doInsert('beforeBegin', el, values, returnElement);
226 <span id='Ext-Template-method-insertAfter'> /**
227 </span> * Applies the supplied values to the template and inserts the new node(s) after el.
228 * @param {Mixed} el The context element
229 * @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'})
230 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
231 * @return {HTMLElement/Ext.core.Element} The new node or Element
233 insertAfter: function(el, values, returnElement) {
234 return this.doInsert('afterEnd', el, values, returnElement);
237 <span id='Ext-Template-method-append'> /**
238 </span> * Applies the supplied <code>values</code> to the template and appends
239 * the new node(s) to the specified <code>el</code>.
240 * <p>For example usage {@link #Template see the constructor}.</p>
241 * @param {Mixed} el The context element
242 * @param {Object/Array} values
243 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
244 * or an object (i.e. <code>{foo: 'bar'}</code>).
245 * @param {Boolean} returnElement (optional) true to return an Ext.core.Element (defaults to undefined)
246 * @return {HTMLElement/Ext.core.Element} The new node or Element
248 append: function(el, values, returnElement) {
249 return this.doInsert('beforeEnd', el, values, returnElement);
252 doInsert: function(where, el, values, returnEl) {
254 var newNode = Ext.core.DomHelper.insertHtml(where, el, this.applyTemplate(values));
255 return returnEl ? Ext.get(newNode, true) : newNode;
258 <span id='Ext-Template-method-overwrite'> /**
259 </span> * Applies the supplied values to the template and overwrites the content of el with the new node(s).
260 * @param {Mixed} el The context element
261 * @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'})
262 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
263 * @return {HTMLElement/Ext.core.Element} The new node or Element
265 overwrite: function(el, values, returnElement) {
267 el.innerHTML = this.applyTemplate(values);
268 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
272 <span id='Ext-Template-method-apply'> /**
273 </span> * Alias for {@link #applyTemplate}
274 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
275 * @param {Object/Array} values
276 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
277 * or an object (i.e. <code>{foo: 'bar'}</code>).
278 * @return {String} The HTML fragment
279 * @member Ext.Template
282 this.createAlias('apply', 'applyTemplate');
284 </pre></pre></body></html>