4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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> * Represents an HTML fragment template. Templates may be {@link #compile precompiled} for greater performance.
21 * An instance of this class may be created by passing to the constructor either a single argument, or multiple
24 * # Single argument: String/Array
26 * The single argument may be either a String or an Array:
30 * var t = new Ext.Template("<div>Hello {0}.</div>");
31 * t.{@link #append}('some-element', ['foo']);
35 * An Array will be combined with `join('')`.
37 * var t = new Ext.Template([
38 * '<div name="{id}">',
39 * '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
42 * t.{@link #compile}();
43 * t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
45 * # Multiple arguments: String, Object, Array, ...
47 * Multiple arguments will be combined with `join('')`.
49 * var t = new Ext.Template(
50 * '<div name="{id}">',
51 * '<span class="{cls}">{name} {value}</span>',
53 * // a configuration object:
55 * compiled: true, // {@link #compile} immediately
61 * - For a list of available format functions, see {@link Ext.util.Format}.
62 * - `disableFormats` reduces `{@link #apply}` time when no formatting is required.
64 Ext.define('Ext.Template', {
66 /* Begin Definitions */
68 requires: ['Ext.DomHelper', 'Ext.util.Format'],
71 <span id='Ext-Template-static-method-from'> /**
72 </span> * Creates a template from the passed element's value (_display:none_ textarea, preferred) or innerHTML.
73 * @param {String/HTMLElement} el A DOM element or its id
74 * @param {Object} config (optional) Config object
75 * @return {Ext.Template} The created template
79 from: function(el, config) {
81 return new this(el.value || el.innerHTML, config || '');
87 <span id='Ext-Template-method-constructor'> /**
88 </span> * Creates new template.
90 * @param {String...} html List of strings to be concatenated into template.
91 * Alternatively an array of strings can be given, but then no config object may be passed.
92 * @param {Object} config (optional) Config object
94 constructor: function(html) {
102 me.initialConfig = {};
105 for (; i < length; i++) {
107 if (typeof value == 'object') {
108 Ext.apply(me.initialConfig, value);
109 Ext.apply(me, value);
114 html = buffer.join('');
116 if (Ext.isArray(html)) {
117 buffer.push(html.join(''));
124 me.html = buffer.join('');
133 <span id='Ext-Template-cfg-compiled'> /**
134 </span> * @cfg {Boolean} compiled
135 * True to immediately compile the template. Defaults to false.
138 <span id='Ext-Template-cfg-disableFormats'> /**
139 </span> * @cfg {Boolean} disableFormats
140 * True to disable format functions in the template. If the template doesn't contain
141 * format functions, setting disableFormats to true will reduce apply time. Defaults to false.
143 disableFormats: false,
145 re: /\{([\w\-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
147 <span id='Ext-Template-method-applyTemplate'> /**
148 </span> * Returns an HTML fragment of this template with the specified values applied.
150 * @param {Object/Array} values The template values. Can be an array if your params are numeric:
152 * var tpl = new Ext.Template('Name: {0}, Age: {1}');
153 * tpl.applyTemplate(['John', 25]);
157 * var tpl = new Ext.Template('Name: {name}, Age: {age}');
158 * tpl.applyTemplate({name: 'John', age: 25});
160 * @return {String} The HTML fragment
162 applyTemplate: function(values) {
164 useFormat = me.disableFormats !== true,
165 fm = Ext.util.Format,
169 return me.compiled(values);
171 function fn(m, name, format, args) {
172 if (format && useFormat) {
174 args = [values[name]].concat(Ext.functionFactory('return ['+ args +'];')());
176 args = [values[name]];
178 if (format.substr(0, 5) == "this.") {
179 return tpl[format.substr(5)].apply(tpl, args);
182 return fm[format].apply(fm, args);
186 return values[name] !== undefined ? values[name] : "";
189 return me.html.replace(me.re, fn);
192 <span id='Ext-Template-method-set'> /**
193 </span> * Sets the HTML used as the template and optionally compiles it.
194 * @param {String} html
195 * @param {Boolean} compile (optional) True to compile the template.
196 * @return {Ext.Template} this
198 set: function(html, compile) {
202 return compile ? me.compile() : me;
206 compileBRe: /(\r\n|\n)/g,
209 <span id='Ext-Template-method-compile'> /**
210 </span> * Compiles the template into an internal function, eliminating the RegEx overhead.
211 * @return {Ext.Template} this
213 compile: function() {
215 fm = Ext.util.Format,
216 useFormat = me.disableFormats !== true,
219 function fn(m, name, format, args) {
220 if (format && useFormat) {
221 args = args ? ',' + args: "";
222 if (format.substr(0, 5) != "this.") {
223 format = "fm." + format + '(';
226 format = 'this.' + format.substr(5) + '(';
231 format = "(values['" + name + "'] == undefined ? '' : ";
233 return "'," + format + "values['" + name + "']" + args + ") ,'";
236 bodyReturn = me.html.replace(me.compileARe, '\\\\').replace(me.compileBRe, '\\n').replace(me.compileCRe, "\\'").replace(me.re, fn);
237 body = "this.compiled = function(values){ return ['" + bodyReturn + "'].join('');};";
242 <span id='Ext-Template-method-insertFirst'> /**
243 </span> * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
245 * @param {String/HTMLElement/Ext.Element} el The context element
246 * @param {Object/Array} values The template values. See {@link #applyTemplate} for details.
247 * @param {Boolean} returnElement (optional) true to return a Ext.Element.
248 * @return {HTMLElement/Ext.Element} The new node or Element
250 insertFirst: function(el, values, returnElement) {
251 return this.doInsert('afterBegin', el, values, returnElement);
254 <span id='Ext-Template-method-insertBefore'> /**
255 </span> * Applies the supplied values to the template and inserts the new node(s) before el.
257 * @param {String/HTMLElement/Ext.Element} el The context element
258 * @param {Object/Array} values The template values. See {@link #applyTemplate} for details.
259 * @param {Boolean} returnElement (optional) true to return a Ext.Element.
260 * @return {HTMLElement/Ext.Element} The new node or Element
262 insertBefore: function(el, values, returnElement) {
263 return this.doInsert('beforeBegin', el, values, returnElement);
266 <span id='Ext-Template-method-insertAfter'> /**
267 </span> * Applies the supplied values to the template and inserts the new node(s) after el.
269 * @param {String/HTMLElement/Ext.Element} el The context element
270 * @param {Object/Array} values The template values. See {@link #applyTemplate} for details.
271 * @param {Boolean} returnElement (optional) true to return a Ext.Element.
272 * @return {HTMLElement/Ext.Element} The new node or Element
274 insertAfter: function(el, values, returnElement) {
275 return this.doInsert('afterEnd', el, values, returnElement);
278 <span id='Ext-Template-method-append'> /**
279 </span> * Applies the supplied `values` to the template and appends the new node(s) to the specified `el`.
281 * For example usage see {@link Ext.Template Ext.Template class docs}.
283 * @param {String/HTMLElement/Ext.Element} el The context element
284 * @param {Object/Array} values The template values. See {@link #applyTemplate} for details.
285 * @param {Boolean} returnElement (optional) true to return an Ext.Element.
286 * @return {HTMLElement/Ext.Element} The new node or Element
288 append: function(el, values, returnElement) {
289 return this.doInsert('beforeEnd', el, values, returnElement);
292 doInsert: function(where, el, values, returnEl) {
294 var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
295 return returnEl ? Ext.get(newNode, true) : newNode;
298 <span id='Ext-Template-method-overwrite'> /**
299 </span> * Applies the supplied values to the template and overwrites the content of el with the new node(s).
301 * @param {String/HTMLElement/Ext.Element} el The context element
302 * @param {Object/Array} values The template values. See {@link #applyTemplate} for details.
303 * @param {Boolean} returnElement (optional) true to return a Ext.Element.
304 * @return {HTMLElement/Ext.Element} The new node or Element
306 overwrite: function(el, values, returnElement) {
308 el.innerHTML = this.applyTemplate(values);
309 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
313 <span id='Ext-Template-method-apply'> /**
314 </span> * @method apply
315 * @member Ext.Template
316 * Alias for {@link #applyTemplate}.
317 * @alias Ext.Template#applyTemplate
319 this.createAlias('apply', 'applyTemplate');