3 * <p>Represents an HTML fragment template. Templates may be {@link #compile precompiled}
4 * for greater performance.</p>
5 * <p>For example usage {@link #Template see the constructor}.</p>
8 * An instance of this class may be created by passing to the constructor either
9 * a single argument, or multiple arguments:
10 * <div class="mdetail-params"><ul>
11 * <li><b>single argument</b> : String/Array
12 * <div class="sub-desc">
13 * The single argument may be either a String or an Array:<ul>
14 * <li><tt>String</tt> : </li><pre><code>
15 var t = new Ext.Template("<div>Hello {0}.</div>");
16 t.{@link #append}('some-element', ['foo']);
18 * <li><tt>Array</tt> : </li>
19 * An Array will be combined with <code>join('')</code>.
21 var t = new Ext.Template([
22 '<div name="{id}">',
23 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
27 t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
30 * <li><b>multiple arguments</b> : String, Object, Array, ...
31 * <div class="sub-desc">
32 * Multiple arguments will be combined with <code>join('')</code>.
34 var t = new Ext.Template(
35 '<div name="{id}">',
36 '<span class="{cls}">{name} {value}</span>',
38 // a configuration object:
40 compiled: true, // {@link #compile} immediately
44 * <p><b>Notes</b>:</p>
45 * <div class="mdetail-params"><ul>
46 * <li>Formatting and <code>disableFormats</code> are not applicable for Sencha Touch.</li>
50 * @param {Mixed} config
52 Ext.Template = function(html) {
57 if (Ext.isArray(html)) {
60 else if (a.length > 1) {
61 Ext.each(a, function(v) {
62 if (Ext.isObject(v)) {
79 Ext.Template.prototype = {
82 re: /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
83 argsRe: /^\s*['"](.*)["']\s*$/,
85 compileBRe: /(\r\n|\n)/g,
89 * @cfg {Boolean} disableFormats true to disable format functions in the template. If the template doesn't contain format functions, setting
90 * disableFormats to true will reduce apply time (defaults to false)
92 disableFormats: false,
95 * Returns an HTML fragment of this template with the specified values applied.
96 * @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'})
97 * @return {String} The HTML fragment
100 applyTemplate: function(values) {
102 useF = me.disableFormats !== true,
103 fm = Ext.util.Format,
110 return me.compiled(values);
112 function fn(m, name, format, args) {
113 if (format && useF) {
114 if (format.substr(0, 5) == "this.") {
115 return tpl.call(format.substr(5), values[name], values);
119 // quoted values are required for strings in compiled templates,
120 // but for non compiled we need to strip them
121 // quoted reversed for jsmin
123 args = args.split(',');
124 for (i = 0, len = args.length; i < len; i++) {
125 args[i] = args[i].replace(re, "$1");
127 args = [values[name]].concat(args);
130 args = [values[name]];
132 return fm[format].apply(fm, args);
136 return values[name] !== undefined ? values[name] : "";
139 return me.html.replace(me.re, fn);
143 * Sets the HTML used as the template and optionally compiles it.
144 * @param {String} html
145 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
146 * @return {Ext.Template} this
148 set: function(html, compile) {
152 return compile ? me.compile() : me;
156 * Compiles the template into an internal function, eliminating the RegEx overhead.
157 * @return {Ext.Template} this
160 compile: function() {
162 fm = Ext.util.Format,
163 useF = me.disableFormats !== true,
166 function fn(m, name, format, args) {
167 if (format && useF) {
168 args = args ? ',' + args: "";
169 if (format.substr(0, 5) != "this.") {
170 format = "fm." + format + '(';
173 format = 'this.call("' + format.substr(5) + '", ';
179 format = "(values['" + name + "'] == undefined ? '' : ";
181 return "'," + format + "values['" + name + "']" + args + ") ,'";
185 body = ["this.compiled = function(values){ return ['"];
186 body.push(me.html.replace(me.compileARe, '\\\\').replace(me.compileBRe, '\\n').replace(me.compileCRe, "\\'").replace(me.re, fn));
187 body.push("'].join('');};");
188 body = body.join('');
194 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
195 * @param {Mixed} el The context element
196 * @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'})
197 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
198 * @return {HTMLElement/Ext.Element} The new node or Element
200 insertFirst: function(el, values, returnElement) {
201 return this.doInsert('afterBegin', el, values, returnElement);
205 * Applies the supplied values to the template and inserts the new node(s) before 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.Element (defaults to undefined)
209 * @return {HTMLElement/Ext.Element} The new node or Element
211 insertBefore: function(el, values, returnElement) {
212 return this.doInsert('beforeBegin', el, values, returnElement);
216 * Applies the supplied values to the template and inserts the new node(s) after 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.Element (defaults to undefined)
220 * @return {HTMLElement/Ext.Element} The new node or Element
222 insertAfter: function(el, values, returnElement) {
223 return this.doInsert('afterEnd', el, values, returnElement);
227 * Applies the supplied <code>values</code> to the template and appends
228 * the new node(s) to the specified <code>el</code>.
229 * <p>For example usage {@link #Template see the constructor}.</p>
230 * @param {Mixed} el The context element
231 * @param {Object/Array} values
232 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
233 * or an object (i.e. <code>{foo: 'bar'}</code>).
234 * @param {Boolean} returnElement (optional) true to return an Ext.Element (defaults to undefined)
235 * @return {HTMLElement/Ext.Element} The new node or Element
237 append: function(el, values, returnElement) {
238 return this.doInsert('beforeEnd', el, values, returnElement);
241 doInsert: function(where, el, values, returnEl) {
243 var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
244 return returnEl ? Ext.get(newNode, true) : newNode;
248 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
249 * @param {Mixed} el The context element
250 * @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'})
251 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
252 * @return {HTMLElement/Ext.Element} The new node or Element
254 overwrite: function(el, values, returnElement) {
256 el.innerHTML = this.applyTemplate(values);
257 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
260 // private function used to call members
261 call: function(fnName, value, allValues) {
262 return this[fnName](value, allValues);
266 * Alias for {@link #applyTemplate}
267 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
268 * @param {Object/Array} values
269 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
270 * or an object (i.e. <code>{foo: 'bar'}</code>).
271 * @return {String} The HTML fragment
272 * @member Ext.Template
275 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;