4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * Represents an HTML fragment template. Templates can be precompiled for greater performance.
15 * For a list of available format functions, see {@link Roo.util.Format}.<br />
18 var t = new Roo.Template({
19 html : '<div name="{id}">' +
20 '<span class="{cls}">{name:trim} {someval:this.myformat}{value:ellipsis(10)}</span>' +
22 myformat: function (value, allValues) {
26 t.append('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
28 * For more information see this blog post with examples: <a href="http://www.jackslocum.com/yui/2006/10/06/domhelper-create-elements-using-dom-html-fragments-or-templates/">DomHelper - Create Elements using DOM, HTML fragments and Templates</a>.
30 * @param {Object} cfg - Configuration object.
32 Roo.Template = function(cfg){
34 if(cfg instanceof Array){
36 }else if(arguments.length > 1){
37 cfg = Array.prototype.join.call(arguments, "");
41 if (typeof(cfg) == 'object') {
52 Roo.Template.prototype = {
55 * @cfg {String} url The Url to load the template from.
59 * @cfg {String} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
63 * Returns an HTML fragment of this template with the specified values applied.
64 * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
65 * @return {String} The HTML fragment
67 applyTemplate : function(values){
71 this.afterLoad = function () { _t.applyTemplate(values); };
76 return this.compiled(values);
78 var useF = this.disableFormats !== true;
79 var fm = Roo.util.Format, tpl = this;
80 var fn = function(m, name, format, args){
82 if(format.substr(0, 5) == "this."){
83 return tpl.call(format.substr(5), values[name], values);
86 // quoted values are required for strings in compiled templates,
87 // but for non compiled we need to strip them
88 // quoted reversed for jsmin
89 var re = /^\s*['"](.*)["']\s*$/;
90 args = args.split(',');
91 for(var i = 0, len = args.length; i < len; i++){
92 args[i] = args[i].replace(re, "$1");
94 args = [values[name]].concat(args);
96 args = [values[name]];
98 return fm[format].apply(fm, args);
101 return values[name] !== undefined ? values[name] : "";
104 return this.html.replace(this.re, fn);
125 this.compiled = false;
127 var cx = new Roo.data.Connection();
131 success : function (response) {
133 _t.html = response.responseText;
135 _t.afterLoad && _t.afterLoad();
137 failure : function(response) {
138 Roo.log("Template failed to load from " + url);
145 * Sets the HTML used as the template and optionally compiles it.
146 * @param {String} html
147 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
148 * @return {Roo.Template} this
150 set : function(html, compile){
152 this.compiled = null;
160 * True to disable format functions (defaults to false)
163 disableFormats : false,
166 * The regular expression used to match template variables
170 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
173 * Compiles the template into an internal function, eliminating the RegEx overhead.
174 * @return {Roo.Template} this
176 compile : function(){
177 var fm = Roo.util.Format;
178 var useF = this.disableFormats !== true;
179 var sep = Roo.isGecko ? "+" : ",";
180 var fn = function(m, name, format, args){
182 args = args ? ',' + args : "";
183 if(format.substr(0, 5) != "this."){
184 format = "fm." + format + '(';
186 format = 'this.call("'+ format.substr(5) + '", ';
190 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
192 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
195 // branched to use + in gecko and [].join() in others
197 body = "this.compiled = function(values){ return '" +
198 this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
201 body = ["this.compiled = function(values){ return ['"];
202 body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
203 body.push("'].join('');};");
204 body = body.join('');
214 // private function used to call members
215 call : function(fnName, value, allValues){
216 return this[fnName](value, allValues);
220 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
221 * @param {String/HTMLElement/Roo.Element} el The context element
222 * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
223 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
224 * @return {HTMLElement/Roo.Element} The new node or Element
226 insertFirst: function(el, values, returnElement){
227 return this.doInsert('afterBegin', el, values, returnElement);
231 * Applies the supplied values to the template and inserts the new node(s) before el.
232 * @param {String/HTMLElement/Roo.Element} el The context element
233 * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
234 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
235 * @return {HTMLElement/Roo.Element} The new node or Element
237 insertBefore: function(el, values, returnElement){
238 return this.doInsert('beforeBegin', el, values, returnElement);
242 * Applies the supplied values to the template and inserts the new node(s) after el.
243 * @param {String/HTMLElement/Roo.Element} el The context element
244 * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
245 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
246 * @return {HTMLElement/Roo.Element} The new node or Element
248 insertAfter : function(el, values, returnElement){
249 return this.doInsert('afterEnd', el, values, returnElement);
253 * Applies the supplied values to the template and appends the new node(s) to el.
254 * @param {String/HTMLElement/Roo.Element} el The context element
255 * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
256 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
257 * @return {HTMLElement/Roo.Element} The new node or Element
259 append : function(el, values, returnElement){
260 return this.doInsert('beforeEnd', el, values, returnElement);
263 doInsert : function(where, el, values, returnEl){
265 var newNode = Roo.DomHelper.insertHtml(where, el, this.applyTemplate(values));
266 return returnEl ? Roo.get(newNode, true) : newNode;
270 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
271 * @param {String/HTMLElement/Roo.Element} el The context element
272 * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
273 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
274 * @return {HTMLElement/Roo.Element} The new node or Element
276 overwrite : function(el, values, returnElement){
278 el.innerHTML = this.applyTemplate(values);
279 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
283 * Alias for {@link #applyTemplate}
286 Roo.Template.prototype.apply = Roo.Template.prototype.applyTemplate;
289 Roo.DomHelper.Template = Roo.Template;
292 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
293 * @param {String/HTMLElement} el A DOM element or its id
294 * @returns {Roo.Template} The created template
297 Roo.Template.from = function(el){
299 return new Roo.Template(el.value || el.innerHTML);