XTemplate.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-XTemplate'>/**
  19. </span> * A template class that supports advanced functionality like:
  20. *
  21. * - Autofilling arrays using templates and sub-templates
  22. * - Conditional processing with basic comparison operators
  23. * - Basic math function support
  24. * - Execute arbitrary inline code with special built-in template variables
  25. * - Custom member functions
  26. * - Many special tags and built-in operators that aren't defined as part of the API, but are supported in the templates that can be created
  27. *
  28. * XTemplate provides the templating mechanism built into {@link Ext.view.View}.
  29. *
  30. * The {@link Ext.Template} describes the acceptable parameters to pass to the constructor. The following examples
  31. * demonstrate all of the supported features.
  32. *
  33. * # Sample Data
  34. *
  35. * This is the data object used for reference in each code example:
  36. *
  37. * var data = {
  38. * name: 'Don Griffin',
  39. * title: 'Senior Technomage',
  40. * company: 'Sencha Inc.',
  41. * drinks: ['Coffee', 'Water', 'More Coffee'],
  42. * kids: [
  43. * { name: 'Aubrey', age: 17 },
  44. * { name: 'Joshua', age: 13 },
  45. * { name: 'Cale', age: 10 },
  46. * { name: 'Nikol', age: 5 },
  47. * { name: 'Solomon', age: 0 }
  48. * ]
  49. * };
  50. *
  51. * # Auto filling of arrays
  52. *
  53. * The **tpl** tag and the **for** operator are used to process the provided data object:
  54. *
  55. * - If the value specified in for is an array, it will auto-fill, repeating the template block inside the tpl
  56. * tag for each item in the array.
  57. * - If for=&quot;.&quot; is specified, the data object provided is examined.
  58. * - While processing an array, the special variable {#} will provide the current array index + 1 (starts at 1, not 0).
  59. *
  60. * Examples:
  61. *
  62. * &lt;tpl for=&quot;.&quot;&gt;...&lt;/tpl&gt; // loop through array at root node
  63. * &lt;tpl for=&quot;foo&quot;&gt;...&lt;/tpl&gt; // loop through array at foo node
  64. * &lt;tpl for=&quot;foo.bar&quot;&gt;...&lt;/tpl&gt; // loop through array at foo.bar node
  65. *
  66. * Using the sample data above:
  67. *
  68. * var tpl = new Ext.XTemplate(
  69. * '&lt;p&gt;Kids: ',
  70. * '&lt;tpl for=&quot;.&quot;&gt;', // process the data.kids node
  71. * '&lt;p&gt;{#}. {name}&lt;/p&gt;', // use current array index to autonumber
  72. * '&lt;/tpl&gt;&lt;/p&gt;'
  73. * );
  74. * tpl.overwrite(panel.body, data.kids); // pass the kids property of the data object
  75. *
  76. * An example illustrating how the **for** property can be leveraged to access specified members of the provided data
  77. * object to populate the template:
  78. *
  79. * var tpl = new Ext.XTemplate(
  80. * '&lt;p&gt;Name: {name}&lt;/p&gt;',
  81. * '&lt;p&gt;Title: {title}&lt;/p&gt;',
  82. * '&lt;p&gt;Company: {company}&lt;/p&gt;',
  83. * '&lt;p&gt;Kids: ',
  84. * '&lt;tpl for=&quot;kids&quot;&gt;', // interrogate the kids property within the data
  85. * '&lt;p&gt;{name}&lt;/p&gt;',
  86. * '&lt;/tpl&gt;&lt;/p&gt;'
  87. * );
  88. * tpl.overwrite(panel.body, data); // pass the root node of the data object
  89. *
  90. * Flat arrays that contain values (and not objects) can be auto-rendered using the special **`{.}`** variable inside a
  91. * loop. This variable will represent the value of the array at the current index:
  92. *
  93. * var tpl = new Ext.XTemplate(
  94. * '&lt;p&gt;{name}\'s favorite beverages:&lt;/p&gt;',
  95. * '&lt;tpl for=&quot;drinks&quot;&gt;',
  96. * '&lt;div&gt; - {.}&lt;/div&gt;',
  97. * '&lt;/tpl&gt;'
  98. * );
  99. * tpl.overwrite(panel.body, data);
  100. *
  101. * When processing a sub-template, for example while looping through a child array, you can access the parent object's
  102. * members via the **parent** object:
  103. *
  104. * var tpl = new Ext.XTemplate(
  105. * '&lt;p&gt;Name: {name}&lt;/p&gt;',
  106. * '&lt;p&gt;Kids: ',
  107. * '&lt;tpl for=&quot;kids&quot;&gt;',
  108. * '&lt;tpl if=&quot;age &amp;gt; 1&quot;&gt;',
  109. * '&lt;p&gt;{name}&lt;/p&gt;',
  110. * '&lt;p&gt;Dad: {parent.name}&lt;/p&gt;',
  111. * '&lt;/tpl&gt;',
  112. * '&lt;/tpl&gt;&lt;/p&gt;'
  113. * );
  114. * tpl.overwrite(panel.body, data);
  115. *
  116. * # Conditional processing with basic comparison operators
  117. *
  118. * The **tpl** tag and the **if** operator are used to provide conditional checks for deciding whether or not to render
  119. * specific parts of the template.
  120. *
  121. * Using the sample data above:
  122. *
  123. * var tpl = new Ext.XTemplate(
  124. * '&lt;p&gt;Name: {name}&lt;/p&gt;',
  125. * '&lt;p&gt;Kids: ',
  126. * '&lt;tpl for=&quot;kids&quot;&gt;',
  127. * '&lt;tpl if=&quot;age &amp;gt; 1&quot;&gt;',
  128. * '&lt;p&gt;{name}&lt;/p&gt;',
  129. * '&lt;/tpl&gt;',
  130. * '&lt;/tpl&gt;&lt;/p&gt;'
  131. * );
  132. * tpl.overwrite(panel.body, data);
  133. *
  134. * More advanced conditionals are also supported:
  135. *
  136. * var tpl = new Ext.XTemplate(
  137. * '&lt;p&gt;Name: {name}&lt;/p&gt;',
  138. * '&lt;p&gt;Kids: ',
  139. * '&lt;tpl for=&quot;kids&quot;&gt;',
  140. * '&lt;p&gt;{name} is a ',
  141. * '&lt;tpl if=&quot;age &amp;gt;= 13&quot;&gt;',
  142. * '&lt;p&gt;teenager&lt;/p&gt;',
  143. * '&lt;tpl elseif=&quot;age &amp;gt;= 2&quot;&gt;',
  144. * '&lt;p&gt;kid&lt;/p&gt;',
  145. * '&lt;tpl else&gt;',
  146. * '&lt;p&gt;baby&lt;/p&gt;',
  147. * '&lt;/tpl&gt;',
  148. * '&lt;/tpl&gt;&lt;/p&gt;'
  149. * );
  150. *
  151. * var tpl = new Ext.XTemplate(
  152. * '&lt;p&gt;Name: {name}&lt;/p&gt;',
  153. * '&lt;p&gt;Kids: ',
  154. * '&lt;tpl for=&quot;kids&quot;&gt;',
  155. * '&lt;p&gt;{name} is a ',
  156. * '&lt;tpl switch=&quot;name&quot;&gt;',
  157. * '&lt;tpl case=&quot;Aubrey&quot; case=&quot;Nikol&quot;&gt;',
  158. * '&lt;p&gt;girl&lt;/p&gt;',
  159. * '&lt;tpl default&gt;',
  160. * '&lt;p&gt;boy&lt;/p&gt;',
  161. * '&lt;/tpl&gt;',
  162. * '&lt;/tpl&gt;&lt;/p&gt;'
  163. * );
  164. *
  165. * A `break` is implied between each case and default, however, multiple cases can be listed
  166. * in a single &amp;lt;tpl&amp;gt; tag.
  167. *
  168. * # Using double quotes
  169. *
  170. * Examples:
  171. *
  172. * var tpl = new Ext.XTemplate(
  173. * &quot;&lt;tpl if='age &amp;gt; 1 &amp;&amp; age &amp;lt; 10'&gt;Child&lt;/tpl&gt;&quot;,
  174. * &quot;&lt;tpl if='age &amp;gt;= 10 &amp;&amp; age &amp;lt; 18'&gt;Teenager&lt;/tpl&gt;&quot;,
  175. * &quot;&lt;tpl if='this.isGirl(name)'&gt;...&lt;/tpl&gt;&quot;,
  176. * '&lt;tpl if=&quot;id == \'download\'&quot;&gt;...&lt;/tpl&gt;',
  177. * &quot;&lt;tpl if='needsIcon'&gt;&lt;img src='{icon}' class='{iconCls}'/&gt;&lt;/tpl&gt;&quot;,
  178. * &quot;&lt;tpl if='name == \&quot;Don\&quot;'&gt;Hello&lt;/tpl&gt;&quot;
  179. * );
  180. *
  181. * # Basic math support
  182. *
  183. * The following basic math operators may be applied directly on numeric data values:
  184. *
  185. * + - * /
  186. *
  187. * For example:
  188. *
  189. * var tpl = new Ext.XTemplate(
  190. * '&lt;p&gt;Name: {name}&lt;/p&gt;',
  191. * '&lt;p&gt;Kids: ',
  192. * '&lt;tpl for=&quot;kids&quot;&gt;',
  193. * '&lt;tpl if=&quot;age &amp;gt; 1&quot;&gt;', // &lt;-- Note that the &gt; is encoded
  194. * '&lt;p&gt;{#}: {name}&lt;/p&gt;', // &lt;-- Auto-number each item
  195. * '&lt;p&gt;In 5 Years: {age+5}&lt;/p&gt;', // &lt;-- Basic math
  196. * '&lt;p&gt;Dad: {parent.name}&lt;/p&gt;',
  197. * '&lt;/tpl&gt;',
  198. * '&lt;/tpl&gt;&lt;/p&gt;'
  199. * );
  200. * tpl.overwrite(panel.body, data);
  201. *
  202. * # Execute arbitrary inline code with special built-in template variables
  203. *
  204. * Anything between `{[ ... ]}` is considered code to be executed in the scope of the template.
  205. * The expression is evaluated and the result is included in the generated result. There are
  206. * some special variables available in that code:
  207. *
  208. * - **out**: The output array into which the template is being appended (using `push` to later
  209. * `join`).
  210. * - **values**: The values in the current scope. If you are using scope changing sub-templates,
  211. * you can change what values is.
  212. * - **parent**: The scope (values) of the ancestor template.
  213. * - **xindex**: If you are in a looping template, the index of the loop you are in (1-based).
  214. * - **xcount**: If you are in a looping template, the total length of the array you are looping.
  215. *
  216. * This example demonstrates basic row striping using an inline code block and the xindex variable:
  217. *
  218. * var tpl = new Ext.XTemplate(
  219. * '&lt;p&gt;Name: {name}&lt;/p&gt;',
  220. * '&lt;p&gt;Company: {[values.company.toUpperCase() + &quot;, &quot; + values.title]}&lt;/p&gt;',
  221. * '&lt;p&gt;Kids: ',
  222. * '&lt;tpl for=&quot;kids&quot;&gt;',
  223. * '&lt;div class=&quot;{[xindex % 2 === 0 ? &quot;even&quot; : &quot;odd&quot;]}&quot;&gt;',
  224. * '{name}',
  225. * '&lt;/div&gt;',
  226. * '&lt;/tpl&gt;&lt;/p&gt;'
  227. * );
  228. *
  229. * Any code contained in &quot;verbatim&quot; blocks (using &quot;{% ... %}&quot;) will be inserted directly in
  230. * the generated code for the template. These blocks are not included in the output. This
  231. * can be used for simple things like break/continue in a loop, or control structures or
  232. * method calls (when they don't produce output). The `this` references the template instance.
  233. *
  234. * var tpl = new Ext.XTemplate(
  235. * '&lt;p&gt;Name: {name}&lt;/p&gt;',
  236. * '&lt;p&gt;Company: {[values.company.toUpperCase() + &quot;, &quot; + values.title]}&lt;/p&gt;',
  237. * '&lt;p&gt;Kids: ',
  238. * '&lt;tpl for=&quot;kids&quot;&gt;',
  239. * '{% if (xindex % 2 === 0) continue; %}',
  240. * '{name}',
  241. * '{% if (xindex &gt; 100) break; %}',
  242. * '&lt;/div&gt;',
  243. * '&lt;/tpl&gt;&lt;/p&gt;'
  244. * );
  245. *
  246. * # Template member functions
  247. *
  248. * One or more member functions can be specified in a configuration object passed into the XTemplate constructor for
  249. * more complex processing:
  250. *
  251. * var tpl = new Ext.XTemplate(
  252. * '&lt;p&gt;Name: {name}&lt;/p&gt;',
  253. * '&lt;p&gt;Kids: ',
  254. * '&lt;tpl for=&quot;kids&quot;&gt;',
  255. * '&lt;tpl if=&quot;this.isGirl(name)&quot;&gt;',
  256. * '&lt;p&gt;Girl: {name} - {age}&lt;/p&gt;',
  257. * '&lt;tpl else&gt;',
  258. * '&lt;p&gt;Boy: {name} - {age}&lt;/p&gt;',
  259. * '&lt;/tpl&gt;',
  260. * '&lt;tpl if=&quot;this.isBaby(age)&quot;&gt;',
  261. * '&lt;p&gt;{name} is a baby!&lt;/p&gt;',
  262. * '&lt;/tpl&gt;',
  263. * '&lt;/tpl&gt;&lt;/p&gt;',
  264. * {
  265. * // XTemplate configuration:
  266. * disableFormats: true,
  267. * // member functions:
  268. * isGirl: function(name){
  269. * return name == 'Aubrey' || name == 'Nikol';
  270. * },
  271. * isBaby: function(age){
  272. * return age &lt; 1;
  273. * }
  274. * }
  275. * );
  276. * tpl.overwrite(panel.body, data);
  277. */
  278. Ext.define('Ext.XTemplate', {
  279. extend: 'Ext.Template',
  280. requires: 'Ext.XTemplateCompiler',
  281. <span id='Ext-XTemplate-property-emptyObj'> /**
  282. </span> * @private
  283. */
  284. emptyObj: {},
  285. <span id='Ext-XTemplate-cfg-compiled'> /**
  286. </span> * @cfg {Boolean} compiled
  287. * Only applies to {@link Ext.Template}, XTemplates are compiled automatically on the
  288. * first call to {@link #apply} or {@link #applyOut}.
  289. */
  290. <span id='Ext-XTemplate-cfg-definitions'> /**
  291. </span> * @cfg {String/Array} definitions
  292. * Optional. A statement, or array of statements which set up `var`s which may then
  293. * be accessed within the scope of the generated function.
  294. */
  295. apply: function(values, parent) {
  296. return this.applyOut(values, [], parent).join('');
  297. },
  298. applyOut: function(values, out, parent) {
  299. var me = this,
  300. compiler;
  301. if (!me.fn) {
  302. compiler = new Ext.XTemplateCompiler({
  303. useFormat: me.disableFormats !== true,
  304. definitions: me.definitions
  305. });
  306. me.fn = compiler.compile(me.html);
  307. }
  308. try {
  309. me.fn.call(me, out, values, parent || me.emptyObj, 1, 1);
  310. } catch (e) {
  311. //&lt;debug&gt;
  312. Ext.log('Error: ' + e.message);
  313. //&lt;/debug&gt;
  314. }
  315. return out;
  316. },
  317. <span id='Ext-XTemplate-method-compile'> /**
  318. </span> * Does nothing. XTemplates are compiled automatically, so this function simply returns this.
  319. * @return {Ext.XTemplate} this
  320. */
  321. compile: function() {
  322. return this;
  323. },
  324. statics: {
  325. <span id='Ext-XTemplate-method-getTpl'> /**
  326. </span> * Gets an `XTemplate` from an object (an instance of an {@link Ext#define}'d class).
  327. * Many times, templates are configured high in the class hierarchy and are to be
  328. * shared by all classes that derive from that base. To further complicate matters,
  329. * these templates are seldom actual instances but are rather configurations. For
  330. * example:
  331. *
  332. * Ext.define('MyApp.Class', {
  333. * someTpl: [
  334. * 'tpl text here'
  335. * ]
  336. * });
  337. *
  338. * The goal being to share that template definition with all instances and even
  339. * instances of derived classes, until `someTpl` is overridden. This method will
  340. * &quot;upgrade&quot; these configurations to be real `XTemplate` instances *in place* (to
  341. * avoid creating one instance per object).
  342. *
  343. * @param {Object} instance The object from which to get the `XTemplate` (must be
  344. * an instance of an {@link Ext#define}'d class).
  345. * @param {String} name The name of the property by which to get the `XTemplate`.
  346. * @return {Ext.XTemplate} The `XTemplate` instance or null if not found.
  347. * @protected
  348. */
  349. getTpl: function (instance, name) {
  350. var tpl = instance[name], // go for it! 99% of the time we will get it!
  351. proto;
  352. if (tpl &amp;&amp; !tpl.isTemplate) { // tpl is just a configuration (not an instance)
  353. // create the template instance from the configuration:
  354. tpl = Ext.ClassManager.dynInstantiate('Ext.XTemplate', tpl);
  355. // and replace the reference with the new instance:
  356. if (instance.hasOwnProperty(name)) { // the tpl is on the instance
  357. instance[name] = tpl;
  358. } else { // must be somewhere in the prototype chain
  359. for (proto = instance.self.prototype; proto; proto = proto.superclass) {
  360. if (proto.hasOwnProperty(name)) {
  361. proto[name] = tpl;
  362. break;
  363. }
  364. }
  365. }
  366. }
  367. // else !tpl (no such tpl) or the tpl is an instance already... either way, tpl
  368. // is ready to return
  369. return tpl || null;
  370. }
  371. }
  372. });
  373. </pre>
  374. </body>
  375. </html>