日期:2014-05-16  浏览次数:20301 次

ExtJs的XTemplate使用

? ExtJs目前还是很多企业web应用首选的js框架,ext本身提供了很多很好的控件给我们使用了,简单的应用已经能基本满足要求,但是,当我们深度使用Ext后,为了应对一些奇怪的需求,就要使用到一些Ext的秘笈了,其中一个就是XTemplate。什么?你还不知道XTemplate是个什么东西?好吧,let go!

?

  • ? 先介绍一下XTemplate

?

?

?

HIERARCHY

Ext.Template
Ext.XTemplate

?

?

XTemplate是Template的一个子类,他能做什么呢?请看docs里面的描述:

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
? 上面的意思就是说,XTemplate就是个模板类,模板是什么?就是结合数据动态生成页面(html)的工具:模板+数据 = HTML。上面还提到这个模板类支持的功能,包括:支持数组的输出、支持条件判断、支持基础的数学运算、支持内建的模板变量....等等

  • ? XTemplate的用法

? 知道XTemplate是什么之后,我们来看看怎么来使用他吧

?

先准备例子数据:

?

?

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 tpl = new Ext.XTemplate(
    '<p>Kids: ',
    '<tpl for="."