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') {
50 Roo.Template.prototype = {
53 * @cfg {String} url The Url to load the template from.
57 * @cfg {String} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
61 * Returns an HTML fragment of this template with the specified values applied.
62 * @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'})
63 * @return {String} The HTML fragment
65 applyTemplate : function(values){
69 return this.compiled(values);
71 var useF = this.disableFormats !== true;
72 var fm = Roo.util.Format, tpl = this;
73 var fn = function(m, name, format, args){
75 if(format.substr(0, 5) == "this."){
76 return tpl.call(format.substr(5), values[name], values);
79 // quoted values are required for strings in compiled templates,
80 // but for non compiled we need to strip them
81 // quoted reversed for jsmin
82 var re = /^\s*['"](.*)["']\s*$/;
83 args = args.split(',');
84 for(var i = 0, len = args.length; i < len; i++){
85 args[i] = args[i].replace(re, "$1");
87 args = [values[name]].concat(args);
89 args = [values[name]];
91 return fm[format].apply(fm, args);
94 return values[name] !== undefined ? values[name] : "";
97 return this.html.replace(this.re, fn);
109 load : function (url, success)
112 this.afterLoad = success;
118 this.compiled = false;
120 var cx = new Roo.data.Connection();
124 success : function (response) {
126 _t.html = response.responseText;
127 _t.afterLoad && _t.afterLoad();
129 failure : function(response) {
130 Roo.log("Template failed to load from " + url);
137 * Sets the HTML used as the template and optionally compiles it.
138 * @param {String} html
139 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
140 * @return {Roo.Template} this
142 set : function(html, compile){
144 this.compiled = null;
152 * True to disable format functions (defaults to false)
155 disableFormats : false,
158 * The regular expression used to match template variables
162 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
165 * Compiles the template into an internal function, eliminating the RegEx overhead.
166 * @return {Roo.Template} this
168 compile : function(){
169 var fm = Roo.util.Format;
170 var useF = this.disableFormats !== true;
171 var sep = Roo.isGecko ? "+" : ",";
172 var fn = function(m, name, format, args){
174 args = args ? ',' + args : "";
175 if(format.substr(0, 5) != "this."){
176 format = "fm." + format + '(';
178 format = 'this.call("'+ format.substr(5) + '", ';
182 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
184 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
187 // branched to use + in gecko and [].join() in others
189 body = "this.compiled = function(values){ return '" +
190 this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
193 body = ["this.compiled = function(values){ return ['"];
194 body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
195 body.push("'].join('');};");
196 body = body.join('');
206 // private function used to call members
207 call : function(fnName, value, allValues){
208 return this[fnName](value, allValues);
212 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
213 * @param {String/HTMLElement/Roo.Element} el The context element
214 * @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'})
215 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
216 * @return {HTMLElement/Roo.Element} The new node or Element
218 insertFirst: function(el, values, returnElement){
219 return this.doInsert('afterBegin', el, values, returnElement);
223 * Applies the supplied values to the template and inserts the new node(s) before el.
224 * @param {String/HTMLElement/Roo.Element} el The context element
225 * @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'})
226 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
227 * @return {HTMLElement/Roo.Element} The new node or Element
229 insertBefore: function(el, values, returnElement){
230 return this.doInsert('beforeBegin', el, values, returnElement);
234 * Applies the supplied values to the template and inserts the new node(s) after el.
235 * @param {String/HTMLElement/Roo.Element} el The context element
236 * @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'})
237 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
238 * @return {HTMLElement/Roo.Element} The new node or Element
240 insertAfter : function(el, values, returnElement){
241 return this.doInsert('afterEnd', el, values, returnElement);
245 * Applies the supplied values to the template and appends the new node(s) to el.
246 * @param {String/HTMLElement/Roo.Element} el The context element
247 * @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'})
248 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
249 * @return {HTMLElement/Roo.Element} The new node or Element
251 append : function(el, values, returnElement){
252 return this.doInsert('beforeEnd', el, values, returnElement);
255 doInsert : function(where, el, values, returnEl){
257 var newNode = Roo.DomHelper.insertHtml(where, el, this.applyTemplate(values));
258 return returnEl ? Roo.get(newNode, true) : newNode;
262 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
263 * @param {String/HTMLElement/Roo.Element} el The context element
264 * @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'})
265 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
266 * @return {HTMLElement/Roo.Element} The new node or Element
268 overwrite : function(el, values, returnElement){
270 el.innerHTML = this.applyTemplate(values);
271 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
275 * Alias for {@link #applyTemplate}
278 Roo.Template.prototype.apply = Roo.Template.prototype.applyTemplate;
281 Roo.DomHelper.Template = Roo.Template;
284 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
285 * @param {String/HTMLElement} el A DOM element or its id
286 * @returns {Roo.Template} The created template
289 Roo.Template.from = function(el){
291 return new Roo.Template(el.value || el.innerHTML);