3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.Template"></div>/**
10 * Represents an HTML fragment template. Templates can be precompiled for greater performance.
11 * For a list of available format functions, see {@link Ext.util.Format}.<br />
14 var t = new Ext.Template(
15 '<div name="{id}">',
16 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
19 t.append('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
22 * @param {String/Array} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
24 Ext.Template = function(html){
29 if (Ext.isArray(html)) {
31 } else if (a.length > 1) {
32 Ext.each(a, function(v) {
33 if (Ext.isObject(v)) {
48 Ext.Template.prototype = {
49 <div id="method-Ext.Template-applyTemplate"></div>/**
50 * Returns an HTML fragment of this template with the specified values applied.
51 * @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'})
52 * @return {String} The HTML fragment
54 applyTemplate : function(values){
59 me.html.replace(me.re, function(m, name){
60 return values[name] !== undefined ? values[name] : "";
64 <div id="method-Ext.Template-set"></div>/**
65 * Sets the HTML used as the template and optionally compiles it.
66 * @param {String} html
67 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
68 * @return {Ext.Template} this
70 set : function(html, compile){
74 return compile ? me.compile() : me;
77 <div id="prop-Ext.Template-re"></div>/**
78 * The regular expression used to match template variables
84 <div id="method-Ext.Template-compile"></div>/**
85 * Compiles the template into an internal function, eliminating the RegEx overhead.
86 * @return {Ext.Template} this
90 sep = Ext.isGecko ? "+" : ",";
93 name = "values['" + name + "']";
94 return "'"+ sep + '(' + name + " == undefined ? '' : " + name + ')' + sep + "'";
97 eval("this.compiled = function(values){ return " + (Ext.isGecko ? "'" : "['") +
98 me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
99 (Ext.isGecko ? "';};" : "'].join('');};"));
103 <div id="method-Ext.Template-insertFirst"></div>/**
104 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
105 * @param {Mixed} el The context element
106 * @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'})
107 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
108 * @return {HTMLElement/Ext.Element} The new node or Element
110 insertFirst: function(el, values, returnElement){
111 return this.doInsert('afterBegin', el, values, returnElement);
114 <div id="method-Ext.Template-insertBefore"></div>/**
115 * Applies the supplied values to the template and inserts the new node(s) before el.
116 * @param {Mixed} el The context element
117 * @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'})
118 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
119 * @return {HTMLElement/Ext.Element} The new node or Element
121 insertBefore: function(el, values, returnElement){
122 return this.doInsert('beforeBegin', el, values, returnElement);
125 <div id="method-Ext.Template-insertAfter"></div>/**
126 * Applies the supplied values to the template and inserts the new node(s) after el.
127 * @param {Mixed} el The context element
128 * @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'})
129 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
130 * @return {HTMLElement/Ext.Element} The new node or Element
132 insertAfter : function(el, values, returnElement){
133 return this.doInsert('afterEnd', el, values, returnElement);
136 <div id="method-Ext.Template-append"></div>/**
137 * Applies the supplied values to the template and appends the new node(s) to el.
138 * @param {Mixed} el The context element
139 * @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'})
140 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
141 * @return {HTMLElement/Ext.Element} The new node or Element
143 append : function(el, values, returnElement){
144 return this.doInsert('beforeEnd', el, values, returnElement);
147 doInsert : function(where, el, values, returnEl){
149 var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
150 return returnEl ? Ext.get(newNode, true) : newNode;
153 <div id="method-Ext.Template-overwrite"></div>/**
154 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
155 * @param {Mixed} el The context element
156 * @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'})
157 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
158 * @return {HTMLElement/Ext.Element} The new node or Element
160 overwrite : function(el, values, returnElement){
162 el.innerHTML = this.applyTemplate(values);
163 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
166 <div id="method-Ext.Template-apply"></div>/**
167 * Alias for {@link #applyTemplate}
168 * Returns an HTML fragment of this template with the specified values applied.
169 * @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'})
170 * @return {String} The HTML fragment
171 * @member Ext.Template
174 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;
176 <div id="method-Ext.Template-Template.from"></div>/**
177 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
178 * @param {String/HTMLElement} el A DOM element or its id
179 * @param {Object} config A configuration object
180 * @return {Ext.Template} The created template
183 Ext.Template.from = function(el, config){
185 return new Ext.Template(el.value || el.innerHTML, config || '');