1:基本知識
XTemplate是Ext.Template擴展的新類,它支持高級功能的模板類,如自動數組輸出、條件判斷、子模板、基本數學運行、特殊內建的模板變量,直接執行代碼和更多的功能
- Autofilling arrays using templates and sub-templates
- Conditional processing with basic comparison operators
- Basic math function support
- Execute arbitrary inline code with special built-in template variables
- Custom member functions
- 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
2:簡單例子1
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>xtemplate</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page"/>
- <script type="text/javascript">
- Ext.onReady(function() {
- //數據源
- var data = {
- name: '博客園',
- read: [{
- book: '開發成功之路---JSP',
- date: '2007-7-7'
- }, {
- book: '大話設計模式',
- date: '2006-6-6'
- }]
- };
- //呈現組件
- var mypanel = new Ext.Panel({
- id: "mypanel",
- width: 300,
- frame: true,
- height: 100,
- title: "XTemplate簡單示例",
- renderTo: Ext.getBody()
- });
- //創建模板
- var tp1 = new Ext.XTemplate(
- '<table width="100%" cellpadding="0" cellspacing="0"><tr><td>編號</td><td>書名</td><td>日期</td></tr>',
- <span style="color: rgb(255, 0, 0);">'<tpl for="read">',</span>
- '<tr>',
- <span style="color: rgb(255, 0, 0);">'<td>{#}</td><td>{book}</td><td>{date}</td></tr>'</span>,
- <span style="color: rgb(255, 0, 0);">'</tpl></table>'</span>
- );
- //重寫綁定模板
- tp1.overwrite(mypanel.body, data);
- });
- </script>
- </head>
- <body>
- </body>
- </html>
程序效果:
上述代碼中使用了標簽tpl和操作符for,可以自由切換for所指定的對象作用域來訪問聲明於模板中的對象,特殊變量#表示當前數組索引+1(由1開始,不是0)
Ext.XTemplate類常用的方法如下:
applay(): 返回值為void,applyTemplate的簡寫形式
compile(): 返回值為Function,把模板編譯成一個函數
form(String/HTMLElement el): 返回值為Ext.Template,從某個元素的value或innerHTML中創建模板
applyTemplate(Object/Array values): 返回值為String, 返回HTML片段,這塊片段是由數據填充模板之后而成
其特性
1:Auto filling of arrays
The tpl tag and the for operator are used to process the provided data object:
- If the value specified in for is an array, it will auto-fill, repeating the template block inside the tpl tag for each item in the array.
- If for="." is specified, the data object provided is examined.
- While processing an array, the special variable {#} will provide the current array index + 1 (starts at 1, not 0)
程序代碼:
- <script type="text/javascript">
- Ext.onReady(function() {
- //數據源
- var data = {
- name: 'Tommy Maintz',
- title: 'Lead Developer',
- company: 'Sencha Inc.',
- email: 'tommy@sencha.com',
- address: '5 Cups Drive',
- city: 'Palo Alto',
- state: 'CA',
- zip: '44102',
- drinks: ['Coffee', 'Soda', 'Water'],
- kids: [{
- name: 'Joshua',
- age:3
- },{
- name: 'Matthew',
- age:2
- },{
- name: 'Solomon',
- age:0
- }]
- };
- //呈現組件
- var mypanel = new Ext.Panel({
- id: "mypanel",
- width: 300,
- frame: true,
- height: 100,
- title: "XTemplate簡單示例",
- renderTo: Ext.getBody()
- });
- //創建模板
- var tpl = new Ext.XTemplate(
- '<p>Kids: ',
- '<tpl for=".">', // process the data.kids node
- '<p>{#}. {name}</p>', // use current array index to autonumber
- '</tpl></p>'
- );
- //重寫綁定模板
- tpl.overwrite(mypanel.body, data.kids); // pass the kids property of the data object
- });
- </script>
- </head>
- <body>
- </body>
程序效果:
2: 修改tp1的內容 An example illustrating how the for property can be leveraged to access specified members of the provided data object to populate the template:
- var tpl = new Ext.XTemplate(
- '<p>Name: {name}</p>',
- '<p>Title: {title}</p>',
- '<p>Company: {company}</p>',
- '<p>Kids: ',
- '<tpl for="kids">', // interrogate the kids property within the data
- '<p>{name}</p>',
- '</tpl></p>'
- );
程序效果:
3:修改tp1的內容, Flat arrays that contain values (and not objects) can be auto-rendered using the special {.} variable inside a loop. This variable will represent the value of the array at the current index:
- var tpl = new Ext.XTemplate(
- '<p>{name}\'s favorite beverages:</p>',
- '<tpl for="drinks">',
- '<div> - {.}</div>',
- '</tpl>'
- );
程序效果:
4: 修改tp1的內容,When processing a sub-template, for example while looping through a child array, you can access the parent object's members via the parent object:
- var tpl = new Ext.XTemplate(
- '<p>Name: {name}</p>',
- '<p>Kids: ',
- '<tpl for="kids">',
- '<tpl if="age > 1">',
- '<p>{name}</p>',
- '<p>Dad: {parent.name}</p>',
- '</tpl>',
- '</tpl></p>'
- );
程序效果:
5:修改tp1的內容 The following basic math operators may be applied directly on numeric data values (+ - * /)
var tpl = new Ext.XTemplate( '<p>Name: {name}</p>', '<p>Kids: ', '<tpl for="kids">', '<tpl if="age > 1">', // <-- Note that the > is encoded '<p>{#}: {name}</p>', // <-- Auto-number each item '<p>In 5 Years: {age+5}</p>', // <-- Basic math '<p>Dad: {parent.name}</p>', '</tpl>', '</tpl></p>' );
程序效果圖:
6:修改tp1的內容 This example demonstrates basic row striping using an inline code block and the xindex variable:
Execute arbitrary inline code with special built-in template variables
Anything between {[ ... ]}
is considered code to be executed in the scope of the template. There are some special variables available in that code:
- values: The values in the current scope. If you are using scope changing sub-templates, you can change what values is.
- parent: The scope (values) of the ancestor template.
- xindex: If you are in a looping template, the index of the loop you are in (1-based).
- xcount: If you are in a looping template, the total length of the array you are looping.
- var tpl = new Ext.XTemplate(
- '<p>Name: {name}</p>',
- '<p>Company: {[values.company.toUpperCase() + ", " + values.title]}</p>',
- '<p>Kids: ',
- '<tpl for="kids">',
- '<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
- '{name}',
- '</div>',
- '</tpl></p>'
- );
CSS樣式為:
- <style type="text/css">
- .even {
- background-color:white;
- }
- .odd {
- background-color: #ffffd9;
- }
- </style>
程序效果圖:
可以看到偶數行和奇數行顯示不同的顏色
7:修改tp1的內容
Template member functions
One or more member functions can be specified in a configuration object passed into the XTemplate constructor for more complex processing:
- var tpl = new Ext.XTemplate(
- '<p>Name: {name}</p>',
- '<p>Kids: ',
- '<tpl for="kids">',
- '<tpl if="this.isGirl(name)">',
- '<p>Girl: {name} - {age}</p>',
- '</tpl>',
- // use opposite if statement to simulate 'else' processing:
- '<tpl if="this.isGirl(name) == false">',
- '<p>Boy: {name} - {age}</p>',
- '</tpl>',
- '<tpl if="this.isBaby(age)">',
- '<p>{name} is a baby!</p>',
- '</tpl>',
- '</tpl></p>',
- {
- // XTemplate configuration:
- compiled: true,
- // member functions:
- isGirl: function(name){
- return name == 'Sara Grace';
- },
- isBaby: function(age){
- return age < 1;
- }
- }
- );
程序效果為: