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 {String} url The Url to load the template from.
61 * @cfg {String} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
65 * Returns an HTML fragment of this template with the specified values applied.
66 * @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'})
67 * @return {String} The HTML fragment
69 applyTemplate : function(values){
74 return this.compiled(values);
76 var useF = this.disableFormats !== true;
77 var fm = Roo.util.Format, tpl = this;
78 var fn = function(m, name, format, args){
80 if(format.substr(0, 5) == "this."){
81 return tpl.call(format.substr(5), values[name], values);
84 // quoted values are required for strings in compiled templates,
85 // but for non compiled we need to strip them
86 // quoted reversed for jsmin
87 var re = /^\s*['"](.*)["']\s*$/;
88 args = args.split(',');
89 for(var i = 0, len = args.length; i < len; i++){
90 args[i] = args[i].replace(re, "$1");
92 args = [values[name]].concat(args);
94 args = [values[name]];
96 return fm[format].apply(fm, args);
99 return values[name] !== undefined ? values[name] : "";
102 return this.html.replace(this.re, fn);
123 this.compiled = false;
125 var cx = new Roo.data.Connection();
129 success : function (response) {
131 _t.html = response.responseText;
133 _t.afterLoad && _t.afterLoad();
135 failure : function(response) {
136 Roo.log("Template failed to load from " + url);
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 {Roo.Template} this
148 set : function(html, compile){
150 this.compiled = null;
158 * True to disable format functions (defaults to false)
161 disableFormats : false,
164 * The regular expression used to match template variables
168 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
171 * Compiles the template into an internal function, eliminating the RegEx overhead.
172 * @return {Roo.Template} this
174 compile : function(){
175 var fm = Roo.util.Format;
176 var useF = this.disableFormats !== true;
177 var sep = Roo.isGecko ? "+" : ",";
178 var fn = function(m, name, format, args){
180 args = args ? ',' + args : "";
181 if(format.substr(0, 5) != "this."){
182 format = "fm." + format + '(';
184 format = 'this.call("'+ format.substr(5) + '", ';
188 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
190 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
193 // branched to use + in gecko and [].join() in others
195 body = "this.compiled = function(values){ return '" +
196 this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
199 body = ["this.compiled = function(values){ return ['"];
200 body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
201 body.push("'].join('');};");
202 body = body.join('');
212 // private function used to call members
213 call : function(fnName, value, allValues){
214 return this[fnName](value, allValues);
218 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
219 * @param {String/HTMLElement/Roo.Element} el The context element
220 * @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'})
221 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
222 * @return {HTMLElement/Roo.Element} The new node or Element
224 insertFirst: function(el, values, returnElement){
225 return this.doInsert('afterBegin', el, values, returnElement);
229 * Applies the supplied values to the template and inserts the new node(s) before el.
230 * @param {String/HTMLElement/Roo.Element} el The context element
231 * @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'})
232 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
233 * @return {HTMLElement/Roo.Element} The new node or Element
235 insertBefore: function(el, values, returnElement){
236 return this.doInsert('beforeBegin', el, values, returnElement);
240 * Applies the supplied values to the template and inserts the new node(s) after el.
241 * @param {String/HTMLElement/Roo.Element} el The context element
242 * @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'})
243 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
244 * @return {HTMLElement/Roo.Element} The new node or Element
246 insertAfter : function(el, values, returnElement){
247 return this.doInsert('afterEnd', el, values, returnElement);
251 * Applies the supplied values to the template and appends the new node(s) to el.
252 * @param {String/HTMLElement/Roo.Element} el The context element
253 * @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'})
254 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
255 * @return {HTMLElement/Roo.Element} The new node or Element
257 append : function(el, values, returnElement){
258 return this.doInsert('beforeEnd', el, values, returnElement);
261 doInsert : function(where, el, values, returnEl){
263 var newNode = Roo.DomHelper.insertHtml(where, el, this.applyTemplate(values));
264 return returnEl ? Roo.get(newNode, true) : newNode;
268 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
269 * @param {String/HTMLElement/Roo.Element} el The context element
270 * @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'})
271 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
272 * @return {HTMLElement/Roo.Element} The new node or Element
274 overwrite : function(el, values, returnElement){
276 el.innerHTML = this.applyTemplate(values);
277 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
281 * Alias for {@link #applyTemplate}
284 Roo.Template.prototype.apply = Roo.Template.prototype.applyTemplate;
287 Roo.DomHelper.Template = Roo.Template;
290 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
291 * @param {String/HTMLElement} el A DOM element or its id
292 * @returns {Roo.Template} The created template
295 Roo.Template.from = function(el){
297 return new Roo.Template(el.value || el.innerHTML);