Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / src / core / Template-more.js
1 /*!
2  * Ext JS Library 3.0.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.Template\r
9  */\r
10 Ext.apply(Ext.Template.prototype, {\r
11     /**\r
12      * Returns an HTML fragment of this template with the specified values applied.\r
13      * @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'})\r
14      * @return {String} The HTML fragment\r
15      * @hide repeat doc\r
16      */\r
17     applyTemplate : function(values){\r
18                 var me = this,\r
19                         useF = me.disableFormats !== true,\r
20                 fm = Ext.util.Format, \r
21                 tpl = me;           \r
22             \r
23         if(me.compiled){\r
24             return me.compiled(values);\r
25         }\r
26         function fn(m, name, format, args){\r
27             if (format && useF) {\r
28                 if (format.substr(0, 5) == "this.") {\r
29                     return tpl.call(format.substr(5), values[name], values);\r
30                 } else {\r
31                     if (args) {\r
32                         // quoted values are required for strings in compiled templates,\r
33                         // but for non compiled we need to strip them\r
34                         // quoted reversed for jsmin\r
35                         var re = /^\s*['"](.*)["']\s*$/;\r
36                         args = args.split(',');\r
37                         for(var i = 0, len = args.length; i < len; i++){\r
38                             args[i] = args[i].replace(re, "$1");\r
39                         }\r
40                         args = [values[name]].concat(args);\r
41                     } else {\r
42                         args = [values[name]];\r
43                     }\r
44                     return fm[format].apply(fm, args);\r
45                 }\r
46             } else {\r
47                 return values[name] !== undefined ? values[name] : "";\r
48             }\r
49         }\r
50         return me.html.replace(me.re, fn);\r
51     },\r
52                 \r
53     /**\r
54      * <tt>true</tt> to disable format functions (defaults to <tt>false</tt>)\r
55      * @type Boolean\r
56      * @property\r
57      */\r
58     disableFormats : false,                             \r
59         \r
60     /**\r
61      * The regular expression used to match template variables\r
62      * @type RegExp\r
63      * @property\r
64      * @hide repeat doc\r
65      */\r
66     re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,\r
67     \r
68     /**\r
69      * Compiles the template into an internal function, eliminating the RegEx overhead.\r
70      * @return {Ext.Template} this\r
71      * @hide repeat doc\r
72      */\r
73     compile : function(){\r
74         var me = this,\r
75                 fm = Ext.util.Format,\r
76                 useF = me.disableFormats !== true,\r
77                 sep = Ext.isGecko ? "+" : ",",\r
78                 body;\r
79         \r
80         function fn(m, name, format, args){\r
81             if(format && useF){\r
82                 args = args ? ',' + args : "";\r
83                 if(format.substr(0, 5) != "this."){\r
84                     format = "fm." + format + '(';\r
85                 }else{\r
86                     format = 'this.call("'+ format.substr(5) + '", ';\r
87                     args = ", values";\r
88                 }\r
89             }else{\r
90                 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";\r
91             }\r
92             return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";\r
93         }\r
94         \r
95         // branched to use + in gecko and [].join() in others\r
96         if(Ext.isGecko){\r
97             body = "this.compiled = function(values){ return '" +\r
98                    me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +\r
99                     "';};";\r
100         }else{\r
101             body = ["this.compiled = function(values){ return ['"];\r
102             body.push(me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));\r
103             body.push("'].join('');};");\r
104             body = body.join('');\r
105         }\r
106         eval(body);\r
107         return me;\r
108     },\r
109     \r
110     // private function used to call members\r
111     call : function(fnName, value, allValues){\r
112         return this[fnName](value, allValues);\r
113     }\r
114 });\r
115 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;