XTemplate能够很方便的在页面中编写一段可以使用数据仓库中数据的html代码。
官网中给出的XTemplate类的一些除了编写html代码之外的方法:
A template class that supports advanced functionality like:
- 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
1.tpl标签和for语句来有选择的控制条件,同时还可以使用if,elseif,else以及switch,case这样的语句来进行条件分支的判断,官方的示例代码:
var tpl = new Ext.XTemplate(
'<p>Name: {name}</p>',
'<p>Kids: ',
'<tpl for="kids">',//for循环
'<p>{name} is a ',
'<tpl if="age >= 13">',
'<p>teenager</p>',
'<tpl elseif="age >= 2">',
'<p>kid</p>',
'<tpl else>',
'<p>baby</p>',
'</tpl>',
'</tpl></p>'
);
var tpl = new Ext.XTemplate(
'<p>Name: {name}</p>',
'<p>Kids: ',
'<tpl for="kids">',
'<p>{name} is a ',
'<tpl switch="name">',//switch语句,发现确实很屌的东西
'<tpl case="Aubrey" case="Nikol">',
'<p>girl</p>',
'<tpl default">',
'<p>boy</p>',
'</tpl>',
'</tpl></p>'
);
2.此外还有很多内置的模版的变量,内置变量的在{[ ]}之间,会被像{ }之间一样被输出;而在{% %}之间的语句则只是用来运行并且作为判断条件。比较常用的有values表示模版正在访问的数据,xindex表示在对数组进行遍历时,显示当前数据项在数组中的序号(第一项为1);xcount当数组进行遍历时,表示数组中数据的条数。
'<tpl for="kids">',
'{% if (xindex % 2 === 0) continue; %}',
'{name}',
'{% if (xindex > 100) break; %}',//可以用来break the loop
'</div>',
'</tpl></p>'
3.在模版中使用自定义的函数,自定义的函数在配置选项中被定义
var tpl = new Ext.XTemplate(
'<tpl for="kids">',
'<tpl if="this.isGirl(name)">',
'<p>Girl: {name} - {age}</p>',
'<tpl else>',
'<p>Boy: {name} - {age}</p>',
'</tpl>',
'</tpl>',
{
// XTemplate configuration:
disableFormats: true,
// member functions:
isGirl: function(name){
return name == 'Sara Grace';
}
}
);
tpl.overwrite(panel.body, data);
分享到:
相关推荐
本文通过对 Sencha Touch 框架的介绍,使读者对该框架有一定的了解,并通过一些代码示例给用户更详细直观的对 Sencha Touch 框架功能的了解。 ExtJS ExtJS 是为 web 开发人员提供的基于 JavaScript 和 web 标准快速...
Sencha Touch开发的项目《般若人生》源码
sencha touch 应用案例 sencha touch 案例 一个完整的sencha touch 应用案例 移动WEB社区开发的日志案例 非常不错!
sencha touch 折叠list
MAC OS 下sencha touch +cordova 开发环境配置。可以将H5 hybird 应用打包成IOS 应用。
sencha touch 2.4.0最新版 原版包下载
sencha touch中文翻译文档,包含sencha touch入门基础和一些控件的使用说明
sencha touch cookbook 英文高清文字版 !
Sencha Touch 2入门教程之MVC 讲解如何使用 Sencha Touch 2 mvc模式来开发一个登录界面
sencha touch list demo
sencha touch动态加载组件及容器内容,防止加载时间过长的问题
讲其解压到Tomcat的webAPP目录下,即可访问
sencha touch 是一个移动平台UI开发框架,可以实现各种绚丽的页面效果
这是我做的一个sencha touch 的项目,测试并能运行,希望对大家有帮助
sencha touch2移动开发框架,ajax获取数据
sencha touch grid
《Sencha Touch 权威指南》陆凌牛著的源码包
sencha touch 2 实现的贪吃蛇 希望对大家有所帮助