SmartTemplate学习笔记

该日志由 Kevin 发表于 2006, May 20, 10:41 AM

目录 
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环境下运行以下程序 

<?php 
    $template  =  new SmartTemplate('template.html'); 
    $text  =  'Sample Text'; 
    $template->assign( 'TITLE', $text ); 
    $template->output(); 
?> 


模版 

<html> {TITLE} </html>
 

输出 

<html> Sample Text </html>
 

在只有一个Array的情况下,可以直接省略前面的array handle,就象在使用javascript时,document.window.close()可以省略为window.close() 

<?php 
    $user  =  array( 
                 'NAME'  => 'John Doe', 
                 'GROUP' => 'Admin', 
                 'AGE'   => '42', 
              ); 
    $template  =  new SmartTemplate('user.html'); 
    $template->assign( $user ); 
    $template->output(); 
?>
 

模版 
 
Name:  {NAME} 
Group: {GROUP} 
Age:   {AGE} 


输出 

Name:  John Doe 
Group: Admin 
Age:   42 


下面是另外一个例子。使用了SmartTemplate的循环函数<!-- begin Array名 -->XXXXXX<!-- end Array名> 
他的功能类似foreach(),只要有东西,就一直循环显示 

<?php 
    $links  =  array( 
                   array( 
                       'TITLE' => 'PHP', 
                       'URL'   => 'http://www.php.net/', 
                   ), 
                   array( 
                       'TITLE' => 'Apache', 
                       'URL'   => 'http://www.php.net/', 
                   ), 
                   array( 
                       'TITLE' => 'MySQL', 
                       'URL'   => 'http://www.mysql.com/', 
                   ), 
               ); 
    $template  =  new SmartTemplate('links.html'); 
    $template->assign( 'links', $links ); 
    $template->output(); 
?> 


HTML模版 

<html> 
<h3> Sample Links </h3> 
<!-- BEGIN links --> 
    <a href="{URL}"> {TITLE} </a> 
<!-- END links --> 
</html> 


<html> 
<h3> Sample Links </h3> 
    <a href="http://www.php.net/"> PHP </a> 
    <a href="http://www.apache.org/"> Apache </a> 
    <a href="http://www.mysql.com/"> MySQL </a> 
</html> 


3、SmartTemplate的逻辑控制结构 

★If和end If 

语法: 
<!-- IF 变量 --> 变量已被赋值! <!-- ENDIF 变量 --> 
如果IF后面直接跟变量,变量为Null时会返回0,否则返回1 

<!-- IF name=="John Doe" --> Your name is John Doe! <!-- ENDIF name --> 


==判断是否相等,如果相等返回1,不相等返回0 

<!-- IF name!="John Doe" --> Your name is not John Doe! <!-- ENDIF name --> 


!=判断是否不等,如果成立返回1,相等则返回0 

例子: 

<?php 
    require_once "class.smarttemplate.php"; 
    $page = new SmartTemplate("if.html"); 
    $page->assign( 'username',   'John Doe' ); 
    $page->assign( 'usergroup',  'ADMIN' ); 
    $page->assign( 'picture',    '' ); 
    $page->output(); 
?>  


HTML 
 
<!-- IF username --> <H3> Welcome, {username} </H3> <!-- ENDIF --> 
<!-- IF picture --> <img src="{picture}"> <!-- ENDIF picture --> 
<!-- IF usergroup="ADMIN" --> 
<a href="admin.php"> ADMIN Login </a><br> 
<!-- ENDIF usergroup --> 


输出

<H3> Welcome, John Doe </H3> 
<a href="admin.php"> ADMIN Login </a><br> 

 
★IF的子局 else 
如果else子句出现在一个逻辑循环中,当if的条件不成立时则会被运行。 
例子 

<?php 
    require_once "class.smarttemplate.php"; 
    $page = new SmartTemplate("else.html"); 
    $page->assign( 'username',   'John Doe' ); 
    $page->assign( 'usergroup',  'ADMIN' ); 
    $page->assign( 'picture',    '' ); 
    $page->output(); 
?> 
 

模版 
 
<!-- IF username --> 
<H3> Welcome, {username} </H3> 
<!-- ENDIF --> 
<!-- IF picture --> 
<img src="{picture}"> 
<!-- ELSE --> 
Picture not available! <br> 
<!-- ENDIF picture --> 
<!-- IF usergroup="ADMIN" --> 
<a href="admin.php"> ADMIN Login </a><br> 
<!-- ELSE --> 
You are in guest mode! 
<!-- ENDIF usergroup -->
 

输出 

<H3> Welcome, John Doe </H3> 
Picture not available! <br> 
<a href="admin.php"> ADMIN Login </a><br>
 

★elseif 

elseif是else和if组合起来的一种结构,其意义为"除此之外如果..." 
以下是一个例子 

<?php 
    require_once "class.smarttemplate.php"; 
    $page = new SmartTemplate("elseif.html"); 
    $page->assign( 'usergroup',  'INTERNAL' ); 
    $page->output(); 
?>
  

模版文件 

<!-- IF usergroup="ADMIN" --> 
<a href="admin.php"> 管理员登陆 </a><br> 
<!-- ELSEIF usergroup="SUPPORT" --> 
<a href="support.php"> 帮助人员登陆</a><br> 
<!-- ELSEIF usergroup --> 
<a href="other.php"> 普通方式登陆 </a><br> 
<!-- ELSE --> 
You don't even have a usergroup! 
<!-- ENDIF -->
 

运行php得到的输出 

<a href="other.php"> 普通方式登陆 </a><br> 


★Begin...End 

这个语句用于读取一个整数索引矩阵(Numerical Array,以数字为索引的数组)的值.而每个整数矩阵的子矩阵则成为以字符串为索引的矩阵(Associative Array)然后在<!-- begin --> 和 <!-- end -->之间的语句将会被读取并且重复执行. 
附加:,每个associative array(字符串为索引的矩阵)会有两个附加的值 
ROWCNT : 此元素在父矩阵中的具体位置 (0,1,2,3,...n) (就是目前在第几个矩阵) 
ROWBIT : 矩阵序号的最后一位. (0,1,0,1,0,1,...) 
下面是一个例子 
 
<?php 
    require_once "class.smarttemplate.php"; 
    $page = new SmartTemplate("begin_end.html"); 
    $users = array( 
               array( 'NAME' => 'John Doe',   'GROUP' => 'ADMIN' ), 
               array( 'NAME' => 'Jack Doe',   'GROUP' => 'SUPPORT' ), 
               array( 'NAME' => 'James Doe',  'GROUP' => 'GUEST' ), 
               array( 'NAME' => 'Jane Doe',   'GROUP' => 'GUEST' ), 
             ); 
    $page->assign( 'users',  $users ); 
    $page->output(); 
?> 
 

HTML模版 

<style type="text/css"> 
.col0 { background-color: #D0D0D0; } 
.col1 { background-color: #F0F0F0; } 
</style> 
<table border="1" cellpadding="2" cellspacing="0"> 
<tr> 
<th> No </th> 
<th> Username </th> 
<th> Usergroup </th> 
</tr> 
<!-- BEGIN users --> 
<tr class="col{ROWBIT}"> 
<td> {ROWCNT} </td> 
<td> {NAME} </td> 
<td> {GROUP} </td> 
</tr> 
<!-- END users --> 
</table> 


最后的输出 
 
<style type="text/css"> 
.col0 { background-color: #D0D0D0; } 
.col1 { background-color: #F0F0F0; } 
</style> 
<table border="1" cellpadding="2" cellspacing="0"> 
<tr> 
<th> No </th> 
<th> Username </th> 
<th> Usergroup </th> 
</tr> 
<tr class="col0"> 
<td> 0 </td> 
<td> John Doe </td> 
<td> ADMIN </td> 
</tr> 
<tr class="col1"> 
<td> 1 </td> 
<td> Jack Doe </td> 
<td> SUPPORT </td> 
</tr> 
<tr class="col0"> 
<td> 2 </td> 
<td> James Doe </td> 
<td> GUEST </td> 
</tr> 
<tr class="col1"> 
<td> 3 </td> 
<td> Jane Doe </td> 
<td> GUEST </td> 
</tr> 
</table> 


☆smartTemplate的方法 

注:以下列出的方法并不会在模版设计中出前,他们属于smartTemplate的代码编辑部分,但是如果为了实现更深一步的模版设计,下面的内容是肯定有用的. 

★基础方法:assign (中文意思:赋值) 

语法: 
assign ( 变量名, 混合内容 ) 
或者 
assign ( 矩阵变量 ) 

更多介绍在变量介绍部分 

★Append(附加) 

语法:append ( 变量名, 内容 ) 

对所提供的变量附加提供的内容 
例子: 

<?php 
    $page  =  new SmartTemplate('links.html'); 
    $page->append('links' => array( 
                                 'TITLE' => 'PHP', 
                                 'URL'   => 'http://www.php.net/' 
                             )); 
    $page->append('links' => array( 
                                 'TITLE' => 'Apache', 
                                 'URL'   => 'http://www.apache.org/' 
                             )); 
    $page->append('links' => array( 
                                 'TITLE' => 'MySQL', 
                                 'URL'   => 'http://www.mysql.com/' 
                             )); 
    $page->output(); 
?> 


模版 
 
<html> 
<h3> Sample Links </h3> 
<!-- BEGIN links --> 
    <a href="{URL}"> {TITLE} </a> 
<!-- END links --> 
</html>
 

最终输出 

<html> 
<h3> Sample Links </h3> 
    <a href="http://www.php.net/"> PHP </a> 
    <a href="http://www.apache.org/"> Apache </a> 
    <a href="http://www.mysql.com/"> MySQL </a> 
</html>
 

另外一个附加字符串的例子: 
 
<?php 
    $page  =  new SmartTemplate('template.html'); 
    $page->append('TITLE' => 'Hello '); 
    $page->append('TITLE' => 'World '); 
    $page->append('TITLE' => '!'); 
    $page->output(); 
?> 


输出将会得到 
 
<html> Hello World ! </html>

网摘: Yahoo Baidu Google Bolaa 365Key Yesky Wozhai POCO ViVi YouNote Hexun Del.icio.us Yeeyoo igooi I2Key Cn3 Bytemen Furl Blinklist Blogmarks Technorati 分享到饭否

« 上一篇: SmartTemplate(适用于企业级PHP开... | 下一篇:PHP,ASP,ASP.NET2.0,JSP运算... »

引用: 点击获得Trackback地址,Encode: UTF-8 点击获得Trackback地址,Encode: GB2312 or GBK 点击获得Trackback地址,Encode: BIG5
发表评论:( 来了就留个脚印吧!你的参与是我最大的动力!)