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:
29 * <a href="http://www.cnitblog.com/seeyeah/archive/2011/12/30/38728.html/">DomHelper
30 - Create Elements using DOM, HTML fragments and Templates</a>.
32 * @param {Object} cfg - Configuration object.
34 Roo.Template = function(cfg){
36 if(cfg instanceof Array){
38 }else if(arguments.length > 1){
39 cfg = Array.prototype.join.call(arguments, "");
43 if (typeof(cfg) == 'object') {
54 Roo.Template.prototype = {
57 * @cfg {Function} onLoad Called after the template has been loaded and complied (usually from a remove source)
63 * @cfg {String} url The Url to load the template from. beware if you are loading from a url, the data may not be ready if you use it instantly..
64 * it should be fixed so that template is observable...
68 * @cfg {String} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
76 * Returns an HTML fragment of this template with the specified values applied.
77 * @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'})
78 * @return {String} The HTML fragment
83 applyTemplate : function(values){
84 //Roo.log(["applyTemplate", values]);
88 return this.compiled(values);
90 var useF = this.disableFormats !== true;
91 var fm = Roo.util.Format, tpl = this;
92 var fn = function(m, name, format, args){
94 if(format.substr(0, 5) == "this."){
95 return tpl.call(format.substr(5), values[name], values);
98 // quoted values are required for strings in compiled templates,
99 // but for non compiled we need to strip them
100 // quoted reversed for jsmin
101 var re = /^\s*['"](.*)["']\s*$/;
102 args = args.split(',');
103 for(var i = 0, len = args.length; i < len; i++){
104 args[i] = args[i].replace(re, "$1");
106 args = [values[name]].concat(args);
108 args = [values[name]];
110 return fm[format].apply(fm, args);
113 return values[name] !== undefined ? values[name] : "";
116 return this.html.replace(this.re, fn);
135 this.compiled = false;
137 var cx = new Roo.data.Connection();
141 success : function (response) {
145 _t.set(response.responseText,true);
151 failure : function(response) {
152 Roo.log("Template failed to load from " + _t.url);
159 * Sets the HTML used as the template and optionally compiles it.
160 * @param {String} html
161 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
162 * @return {Roo.Template} this
164 set : function(html, compile){
166 this.compiled = false;
174 * True to disable format functions (defaults to false)
177 disableFormats : false,
180 * The regular expression used to match template variables
184 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
187 * Compiles the template into an internal function, eliminating the RegEx overhead.
188 * @return {Roo.Template} this
190 compile : function(){
191 var fm = Roo.util.Format;
192 var useF = this.disableFormats !== true;
193 var sep = Roo.isGecko ? "+" : ",";
194 var fn = function(m, name, format, args){
196 args = args ? ',' + args : "";
197 if(format.substr(0, 5) != "this."){
198 format = "fm." + format + '(';
200 format = 'this.call("'+ format.substr(5) + '", ';
204 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
206 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
209 // branched to use + in gecko and [].join() in others
211 body = "this.compiled = function(values){ return '" +
212 this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
215 body = ["this.compiled = function(values){ return ['"];
216 body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
217 body.push("'].join('');};");
218 body = body.join('');
228 // private function used to call members
229 call : function(fnName, value, allValues){
230 return this[fnName](value, allValues);
234 * Applies the supplied values to the template and inserts the new node(s) as the first child of 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 insertFirst: function(el, values, returnElement){
241 return this.doInsert('afterBegin', el, values, returnElement);
245 * Applies the supplied values to the template and inserts the new node(s) before 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 insertBefore: function(el, values, returnElement){
252 return this.doInsert('beforeBegin', el, values, returnElement);
256 * Applies the supplied values to the template and inserts the new node(s) after el.
257 * @param {String/HTMLElement/Roo.Element} el The context element
258 * @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'})
259 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
260 * @return {HTMLElement/Roo.Element} The new node or Element
262 insertAfter : function(el, values, returnElement){
263 return this.doInsert('afterEnd', el, values, returnElement);
267 * Applies the supplied values to the template and appends the new node(s) to el.
268 * @param {String/HTMLElement/Roo.Element} el The context element
269 * @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'})
270 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
271 * @return {HTMLElement/Roo.Element} The new node or Element
273 append : function(el, values, returnElement){
274 return this.doInsert('beforeEnd', el, values, returnElement);
277 doInsert : function(where, el, values, returnEl){
279 var newNode = Roo.DomHelper.insertHtml(where, el, this.applyTemplate(values));
280 return returnEl ? Roo.get(newNode, true) : newNode;
284 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
285 * @param {String/HTMLElement/Roo.Element} el The context element
286 * @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'})
287 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
288 * @return {HTMLElement/Roo.Element} The new node or Element
290 overwrite : function(el, values, returnElement){
292 el.innerHTML = this.applyTemplate(values);
293 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
297 * Alias for {@link #applyTemplate}
300 Roo.Template.prototype.apply = Roo.Template.prototype.applyTemplate;
303 Roo.DomHelper.Template = Roo.Template;
306 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
307 * @param {String/HTMLElement} el A DOM element or its id
308 * @returns {Roo.Template} The created template
311 Roo.Template.from = function(el){
313 return new Roo.Template(el.value || el.innerHTML);