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