bsp; < /table >
< !-- END: table.tpl -- >
< !-- NAME: row.tpl -- >
< tr >
< td >{NUMBER}< /td >
< td >{BIG_NUMBER}< /td >
< /tr >
< !-- END: row.tpl -- >
使用这个库首先要如前面所说的 include "class.FastTemplate.php3"; 然后是定义模板所在目录$tpl = new FastTemplate("./templates");注意哟!这里因为我是在windows系统运行,所以用的是"./templates",在Linux或UNIX中可能不一样,大家根据具体情况来设定。然后呢,我们对应不同的模板,定义下面这个矩阵数组。$tpl- >define( array( main = > "main.tpl", table = > "table.tpl", row = > "row.tpl" ) ); define是这个类中的一个函数。下面是它的语法:define( array( key,value pairs) ) ,define()函数映射一个名字到模板文件上,这个新的名字将是你用来代表模板的唯一名字,因为除此之外再不会有模板文件名出现。而且大家注意,这是使用类时决不能缺少的步骤,不能少的哟,如果少了那就和你不带生日礼物去你女朋友的生日Party有异曲同工之妙,嘿嘿嘿~~~~
现在!关键的,最有个性的东西来了!assign( (key,value pair) 或 assign ( array(key value pairs) ) 这个函数将把你在模板中定义的标识符定义为你网页上真正想要的东西。比如就象是上面$tpl- >assign( array( TITLE = > "FastTemplate Test") ); 这句,将模板main.tpl里的{TITLE}替换为 FastTemplate Test 看不懂想不明的朋友多看看上面的那些代码。接着呢!接着是另外一个很有特色的东东。parse(RETURN, FileHandle(s) ) 将一个已定义模板插入的定义到另外一个模板文件中。大家仔细研究下面的代码,相信会比较容易了解。
for ($n=1; $n < = 3; $n++)
{
$Number = $n;
$BigNum = $n*10;
$tpl- >assign(
array(
NUMBER = > $Number,
BIG_NUMBER = > $BigNum
)
);
$tpl- >parse(ROWS,".row");
}
parse这个函数有三种用法
$tpl- >parse(MAIN, "main"); // 标准
$tpl- >parse(MAIN, array ( "table", "main") ); // 简洁
$tpl- >parse(MAIN, ".row"); // 增加
其中以第三种最有意思,它表示的是在原来已经有的基础上再加上一个新数据。呵呵,这么说大家可能无法明白,我们还是看看再多研究一下上面的源代码吧,毕竟编程这东西有很多是只可意会不可言传的!(下面附上英文的说明,不是地藏想偷懒不翻译,实在是有些东西看原味的比翻译的更加容易理解呀,
In the regular version, the template named ``main'' is loaded if it hasn't been already, all the variables are interpolated, and the result is then stored in FastTemplate as the value MAIN. If the variable '{MAIN}' shows up in a later template, it will be interpolated to be the value of the parsed ``main'' template. This allows you to easily nest templates, which brings us to the compound style.
The compound style is designed to make it easier to nest templates.
The following are equivalent:
$tpl- >parse(MAIN, "table");
$tpl- >parse(MAIN, ".main");
// is the same as:
$tpl- >parse(MAIN, array("table", "main"));
// this form saves func
< !-- END: table.tpl -- >
< !-- NAME: row.tpl -- >
< tr >
< td >{NUMBER}< /td >
< td >{BIG_NUMBER}< /td >
< /tr >
< !-- END: row.tpl -- >
使用这个库首先要如前面所说的 include "class.FastTemplate.php3"; 然后是定义模板所在目录$tpl = new FastTemplate("./templates");注意哟!这里因为我是在windows系统运行,所以用的是"./templates",在Linux或UNIX中可能不一样,大家根据具体情况来设定。然后呢,我们对应不同的模板,定义下面这个矩阵数组。$tpl- >define( array( main = > "main.tpl", table = > "table.tpl", row = > "row.tpl" ) ); define是这个类中的一个函数。下面是它的语法:define( array( key,value pairs) ) ,define()函数映射一个名字到模板文件上,这个新的名字将是你用来代表模板的唯一名字,因为除此之外再不会有模板文件名出现。而且大家注意,这是使用类时决不能缺少的步骤,不能少的哟,如果少了那就和你不带生日礼物去你女朋友的生日Party有异曲同工之妙,嘿嘿嘿~~~~
现在!关键的,最有个性的东西来了!assign( (key,value pair) 或 assign ( array(key value pairs) ) 这个函数将把你在模板中定义的标识符定义为你网页上真正想要的东西。比如就象是上面$tpl- >assign( array( TITLE = > "FastTemplate Test") ); 这句,将模板main.tpl里的{TITLE}替换为 FastTemplate Test 看不懂想不明的朋友多看看上面的那些代码。接着呢!接着是另外一个很有特色的东东。parse(RETURN, FileHandle(s) ) 将一个已定义模板插入的定义到另外一个模板文件中。大家仔细研究下面的代码,相信会比较容易了解。
for ($n=1; $n < = 3; $n++)
{
$Number = $n;
$BigNum = $n*10;
$tpl- >assign(
array(
NUMBER = > $Number,
BIG_NUMBER = > $BigNum
)
);
$tpl- >parse(ROWS,".row");
}
parse这个函数有三种用法
$tpl- >parse(MAIN, "main"); // 标准
$tpl- >parse(MAIN, array ( "table", "main") ); // 简洁
$tpl- >parse(MAIN, ".row"); // 增加
其中以第三种最有意思,它表示的是在原来已经有的基础上再加上一个新数据。呵呵,这么说大家可能无法明白,我们还是看看再多研究一下上面的源代码吧,毕竟编程这东西有很多是只可意会不可言传的!(下面附上英文的说明,不是地藏想偷懒不翻译,实在是有些东西看原味的比翻译的更加容易理解呀,
In the regular version, the template named ``main'' is loaded if it hasn't been already, all the variables are interpolated, and the result is then stored in FastTemplate as the value MAIN. If the variable '{MAIN}' shows up in a later template, it will be interpolated to be the value of the parsed ``main'' template. This allows you to easily nest templates, which brings us to the compound style.
The compound style is designed to make it easier to nest templates.
The following are equivalent:
$tpl- >parse(MAIN, "table");
$tpl- >parse(MAIN, ".main");
// is the same as:
$tpl- >parse(MAIN, array("table", "main"));
// this form saves func
| 对此文章发表了评论 |
