目录
1、SmartTemplate的效率
2、基本变量
3、逻辑运算结构
4、模式(Methods)
5、扩展类(Extensions) 未完成
------------------------------------------------------------------------
1、SmartTemplate的效率
虽然他有很多的程序来形成强大的功能,但在执行时只有你调用的才被导入,所以不用担心这方面的速度影响,同样这套模版系统是为最快的执行效率而优化过的,比起目前市场上常见的Smarty,要快不少(Smarty采用后期缓存,所以比较可能不是很准确)。
2、SmartTemplate的变量
Array的变量是由SmartTemplate内建函数assign()来赋值的
具体语法如下
assign ( 模版中的变量, 要替换的内容 )
或
assign ( Array内容 )
正如其他程序的变量一样,smartTemplate的变量是由特殊的{}所包含的。里面的内容可以是String,Array,Int,或者是Long Text等等(基本上php支持的)
在储存Array数据时,smartTemplate运用了我们常见的父子级分割符".",所以一个特殊的Array数据由Array Handle和具体位置的索引组成(Numerical Index or Associative Index)。
下面是一个例子
在php环境下运行以下程序
代码:
1
2
3$template = new SmartTemplate('template.html');
4
5$text = 'Sample Text';
6$template->assign( 'TITLE', $text );
7
8$template->output();
9
模版
代码:
1<html> {TITLE} </html>
输出
代码:
1<html> Sample Text </html>
在只有一个Array的情况下,可以直接省略前面的array handle,就象在使用javascript时,document.window.close()可以省略为window.close()
php
代码:
1
2
3$user = array(
4'NAME' => 'John Doe',
5'GROUP' => 'Admin',
6'AGE' => '42',
7);
8
9$template = new SmartTemplate('user.html');
10
11$template->assign( $user );
12
13$template->output();
14
模版
代码:
Name: {NAME}
Group: {GROUP}
Age: {AGE}
输出
代码:
Name: John Doe
Group: Admin
Age: 42
下面是另外一个例子。使用了SmartTemplate的循环函数
XXXXXX
1<a href="{URL}"> {TITLE} </a>
代码:
1<html>
2<h3> Sample Links </h3>
3<a href="http://www.php.net/"> PHP </a>
4<a href="http://www.apache.org/"> Apache </a>
5<a href="http://www.mysql.com/"> MySQL </a>
6</html>
3、SmartTemplate的逻辑控制结构
★If和end If
语法:
变量已被赋值!
如果IF后面直接跟变量,变量为Null时会返回0,否则返回1
Your name is John Doe!
==判断是否相等,如果相等返回1,不相等返回0
Your name is not John Doe!
!=判断是否不等,如果成立返回1,相等则返回0
例子:
PHP
代码:
1
2
3require_once "class.smarttemplate.php";
4$page = new SmartTemplate("if.html");
5
6$page->assign( 'username', 'John Doe' );
7$page->assign( 'usergroup', 'ADMIN' );
8$page->assign( 'picture', '' );
9
10$page->output();
11
HTML
代码:
1<h3> Welcome, {username} </h3>
1<img src="{picture}"/>
1<a href="admin.php"> ADMIN Login </a>
1<br/>
输出代码:
1<h3> Welcome, John Doe </h3>
1<a href="admin.php"> ADMIN Login </a>
1<br/>
★IF的子局 else
如果else子句出现在一个逻辑循环中,当if的条件不成立时则会被运行。
例子
代码:
1
2
3require_once "class.smarttemplate.php";
4$page = new SmartTemplate("else.html");
5
6$page->assign( 'username', 'John Doe' );
7$page->assign( 'usergroup', 'ADMIN' );
8$page->assign( 'picture', '' );
9
10$page->output();
11
模版
代码:
1<h3> Welcome, {username} </h3>
1<img src="{picture}"/>
Picture not available!
1<br/>
1<a href="admin.php"> ADMIN Login </a>
1<br/>
You are in guest mode!
输出
代码:
1<h3> Welcome, John Doe </h3>
Picture not available!
1<br/>
1<a href="admin.php"> ADMIN Login </a>
1<br/>
★elseif
elseif是else和if组合起来的一种结构,其意义为"除此之外如果..."
以下是一个例子
代码:
1
2
3require_once "class.smarttemplate.php";
4$page = new SmartTemplate("elseif.html");
5
6$page->assign( 'usergroup', 'INTERNAL' );
7
8$page->output();
9
模版文件
代码:
1<a href="admin.php"> 管理员登陆 </a>
1<br/>
1<a href="support.php"> 帮助人员登陆</a>
1<br/>
1<a href="other.php"> 普通方式登陆 </a>
1<br/>
You don't even have a usergroup!
运行php得到的输出
代码:
1<a href="other.php"> 普通方式登陆 </a>
1<br/>
★Begin...End
这个语句用于读取一个整数索引矩阵(Numerical Array,以数字为索引的数组)的值.而每个整数矩阵的子矩阵则成为以字符串为索引的矩阵(Associative Array)然后在
和
之间的语句将会被读取并且重复执行.
附加:,每个associative array(字符串为索引的矩阵)会有两个附加的值
ROWCNT : 此元素在父矩阵中的具体位置 (0,1,2,3,...n) (就是目前在第几个矩阵)
ROWBIT : 矩阵序号的最后一位. (0,1,0,1,0,1,...)
下面是一个例子
PHP代码:
代码:
1
2
3require_once "class.smarttemplate.php";
4$page = new SmartTemplate("begin_end.html");
5
6$users = array(
7array( 'NAME' => 'John Doe', 'GROUP' => 'ADMIN' ),
8array( 'NAME' => 'Jack Doe', 'GROUP' => 'SUPPORT' ),
9array( 'NAME' => 'James Doe', 'GROUP' => 'GUEST' ),
10array( 'NAME' => 'Jane Doe', 'GROUP' => 'GUEST' ),
11);
12
13$page->assign( 'users', $users );
14
15$page->output();
16
HTML模版
代码:
1<style type="text/css">
2.col0 { background-color: #D0D0D0; }
3.col1 { background-color: #F0F0F0; }
4</style>
1<table border="1" cellpadding="2" cellspacing="0">
2<tr>
3<th> No </th>
4<th> Username </th>
5<th> Usergroup </th>
6</tr>
7<!-- BEGIN users -->
8<tr class="col{ROWBIT}">
9<td> {ROWCNT} </td>
10<td> {NAME} </td>
11<td> {GROUP} </td>
12</tr>
13<!-- END users -->
14</table>
最后的输出
代码:
1<style type="text/css">
2.col0 { background-color: #D0D0D0; }
3.col1 { background-color: #F0F0F0; }
4</style>
1<table border="1" cellpadding="2" cellspacing="0">
2<tr>
3<th> No </th>
4<th> Username </th>
5<th> Usergroup </th>
6</tr>
7<tr class="col0">
8<td> 0 </td>
9<td> John Doe </td>
10<td> ADMIN </td>
11</tr>
12<tr class="col1">
13<td> 1 </td>
14<td> Jack Doe </td>
15<td> SUPPORT </td>
16</tr>
17<tr class="col0">
18<td> 2 </td>
19<td> James Doe </td>
20<td> GUEST </td>
21</tr>
22<tr class="col1">
23<td> 3 </td>
24<td> Jane Doe </td>
25<td> GUEST </td>
26</tr>
27</table>
☆smartTemplate的方法
注:以下列出的方法并不会在模版设计中出前,他们属于smartTemplate的代码编辑部分,但是如果为了实现更深一步的模版设计,下面的内容是肯定有用的.
★基础方法:assign (中文意思:赋值)
语法:
assign ( 变量名, 混合内容 )
或者
assign ( 矩阵变量 )
更多介绍在变量介绍部分
★Append(附加)
语法:append ( 变量名, 内容 )
对所提供的变量附加提供的内容
例子:
代码:
1
2
3$page = new SmartTemplate('links.html');
4
5$page->append('links' => array(
6'TITLE' => 'PHP',
7'URL' => 'http://www.php.net/'
8));
9$page->append('links' => array(
10'TITLE' => 'Apache',
11'URL' => 'http://www.apache.org/'
12));
13$page->append('links' => array(
14'TITLE' => 'MySQL',
15'URL' => 'http://www.mysql.com/'
16));
17$page->output();
18
模版
代码:
1<html>
2<h3> Sample Links </h3>
3<!-- BEGIN links -->
4<a href="{URL}"> {TITLE} </a>
5<!-- END links -->
6</html>
最终输出
代码:
1<html>
2<h3> Sample Links </h3>
3<a href="http://www.php.net/"> PHP </a>
4<a href="http://www.apache.org/"> Apache </a>
5<a href="http://www.mysql.com/"> MySQL </a>
6</html>
另外一个附加字符串的例子:
代码:
1
2
3$page = new SmartTemplate('template.html');
4
5$page->append('TITLE' => 'Hello ');
6$page->append('TITLE' => 'World ');
7$page->append('TITLE' => '!');
8
9$page->output();
10
输出将会得到
代码:
1<html> Hello World ! </html>