`
victorwmh
  • 浏览: 207611 次
  • 性别: Icon_minigender_1
  • 来自: 宇宙
社区版块
存档分类
最新评论

SmartTemplate

    博客分类:
  • php
 
阅读更多

 

 

基本方法 

 

SmartTemplate::assign() 
void assign ( string PLACEHOLDER, mixed CONTENT ) or void assign ( array CONTENT ) 
给模板占位符(PLACEHOLDER)或者列表(CONTENT)赋值. 可以使用散列数组或者标量  
   
例子1:标量赋值  

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



模板(template.html):   

<html> {TITLE} </html>



输出:   

<html> Sample Text </html>



例子2: 多个标量赋值  

<?php 
    $template  =  new SmartTemplate('user.html'); 

    $template->assign( 'NAME',  'John Doe' ); 
    $template->assign( 'GROUP', 'Admin'    ); 
    $template->assign( 'AGE',   '42'       ); 

    $template->output(); 
?>




模板(user.html):   

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



输出:   

Name:  John Doe 
Group: Admin 
Age:   42



例子3: 使用数组给多个标量赋值  

<?php 
    $user  =  array( 
                 'NAME'  => 'John Doe',  
                 'GROUP' => 'Admin', 
                 'AGE'   => '42', 
              ); 

    $template  =  new SmartTemplate('user.html'); 
    $template->assign( $user ); 
    $template->output(); 
?>




模板(user.html):   

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



输出:   

Name:  John Doe 
Group: Admin 
Age:   42


   
例子4: 命名空间  

<?php 
    $admin  =  array( 
                   'NAME'  => 'John Doe',  
                   'AGE'   => '42', 
               ); 
    $guest  =  array( 
                   'NAME'  => 'Roger Rabbit',  
                   'AGE'   => '16', 
               ); 

    $template  =  new SmartTemplate('users.html'); 

    $template->assign( 'admin', $admin ); 
    $template->assign( 'guest', $guest ); 

    $template->output(); 
?>



模板(user.html): 占位符(PLACEHOLDER)对应数组,“.”对应数组“[]”  

Admin Name: {admin.NAME} 
Admin Age:  {admin.AGE} 

Guest Name: {guest.NAME} 
Guest Age:  {guest.AGE}



输出:   

Admin Name: John Doe 
Admin Age:  42 

Guest Name: Roger Rabbit 
Guest Age:  16



例子5: 使用数组命名空间  

<?php 
    $users  =  array( 
                   'admin' => array( 
                       'NAME'  => 'John Doe',  
                       'AGE'   => '42', 
                   ), 
                   'guest' => array( 
                       'NAME'  => 'Roger Rabbit',  
                       'AGE'   => '16', 
                   ), 
               ); 
    $template  =  new SmartTemplate('users.html'); 
    $template->assign( $users ); 
    $template->output(); 
?>



模板(user.html): 占位符(PLACEHOLDER)对应数组,“.”对应数组“[]”  

Admin Name: {admin.NAME} 
Admin Age:  {admin.AGE} 

Guest Name: {guest.NAME} 
Guest Age:  {guest.AGE}



输出:   

Admin Name: John Doe 
Admin Age:  42 

Guest Name: Roger Rabbit 
Guest Age:  16



例子6: 命名空间, 3个部分  

<?php 
    $template  =  new SmartTemplate('template.html'); 
    $content['world']['europe']['germany']  =  'DE'; 
    $template->assign( 'top_level_domain', $content ); 
    $template->output(); 
?>



模板(template.html): 占位符(PLACEHOLDER)对应数组,“.”对应数组“[]”  

<html>German TLD: {top_level_domain.world.europe.germany} </html>



输出:   

<html>German TLD: DE </html>


   
例子7: 列表赋值  

<?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(); 
?>



模板(links.html): 结构名称lnks对应数组  

<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>


   
Example 8: 使用数组于多个命名空间  

<?php 
    $title  =  'Sample Links';  //  Page Title 
    $target =  '_blank';        //  The Same Target for all links 
    $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( 'TITLE', $title ); 
    $template->assign( 'TARGET', $target ); 
    $template->assign( 'links',  $links  ); 
    $template->output(); 
?>



注意:  

TITLE 与 links..TITLE 使用不同的命名空间!  
TARGET 不是 links 数组的成员. 如果使用在 BEGIN..END 块之内, 他必须被引用为 {parent.TARGET} 或者 {top.TARGET}.  

其他可能的用法:   

{top.TITLE}, {parent.parent.PAGE_ID}, {top.users.ADMIN}, 等等..  

模板(links.html):   

<html> 
<h3> {TITLE} </h3> 
<!-- BEGIN links --> 
    <a target='{parent.TARGET}' href="{URL}"> {TITLE} </a> 
<!-- END links --> 
</html>



输出:   

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



SmartTemplate::append() void append ( string PLACEHOLDER, mixed CONTENT )  
追加内容给模板占位符. 可以使用散列数组或者标量.  

例子1 (列表):  

<?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(); 
?>



模板(links.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>



例子2 (标量):  

<?php 
    $page  =  new SmartTemplate('template.html'); 
    $page->append('TITLE' => 'Hello '); 
    $page->append('TITLE' => 'World '); 
    $page->append('TITLE' => '!'); 
    $page->output(); 
?>



模板(template.html): 标量为内容的追加  

<html> {TITLE} </html>



输出:   

<html> Hello World !</html>



SmartTemplate::output() void output () 
解析模板并输出结果.  

例子:   

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



SmartTemplate::result() string result () 
解析模板并返回结果.  
例子:   

<?php 
    $page  =  new SmartTemplate('template.html'); 
    $page->assign('TITLE' => 'Sample Title'); 
    $output  =  $page->result(); 
    echo 'Output page: ' . $output; 
?>



SmartTemplate::use_cache void use_cache ( [mixed key] )  

激活内建的输出缓存. 判断当前执行的脚本 (判断依据$_SERVER[REQUEST URI]) 是否在确定的时间内执行过. 如果执行过, use_cache 将返回缓存的页面给浏览器并且中止运行.  
如果没有一个有效的输出句柄可以使用,use_cache将激活PHP输出缓存,并且返回数据到执行它的脚本. 下面的脚本执行时, use_cache 捕获所有输出到浏览器的内容,并保存到缓存目录. 缓存的每一个文件名称是唯一的,他根据当前执行的脚本文件名称,GET参数(REQUEST_URI)以及可选得参数来自东设定.  
如果脚本有一些重要的工作,例如记录日志等,那么应该在use_cache 之前调用你的代码.  
例子:   

<?php 
    $page  =  SmartTemplate('template.html'); 
    $page->cache_dir       =  '/tmp/';  //  Where to store cache files 
    $page->cache_lifetime  =  120;      //  Keep cache for 120 seconds 
    $page->use_cache();                 //  Activate ouput cache 
    // 
    //  Assemble Page Content 
    // 
    $page->output(); 
?>



SmartTemplate::debug() void debug ()  

激活内建调试器. Debug 能够代替或者内嵌在 output . 他列出了指定的变量及其内容的详细列表, 编译后的模板和模板的原来结构.  
Debug 对于确定和排除模板中的错误非常有用. 

流程控制  

SmartPHP 例子: if 
if ... endif 控制有条件的输出模板的部分. 
 

语法如下:  
变量不为空  

<!-- IF var --> var 不为空! <!-- ENDIF var -->


   
变量值判断  

<!-- IF name=="HonestQiao" --> Your name is HonestQiao! <!-- ENDIF name -->


   
变量值否定判断  

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



(var 在 ENDIF 之后是可选的,但是最好加上)  

 if.php:    

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



if.php使用的模板文件如下:  
 if.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 -->



if.php执行的效果如下:  
 输出: (  查看)   

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



SmartPHP 例子: else 
else 控制作为 if 控制的扩展,当if 判断结果为 FALSE 来输出模板的一部分. 
 

 else.php: 

<?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(); 
?> 



else.php使用的模板文件如下:  
 else.html:    

<!-- 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 -->



else.php执行的效果如下:  
 输出: (  查看)   

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



SmartPHP 例子: elseif 
elseif 控制是 else 与 if 的结合. 
 

 elseif.php: (  下载)   

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



elseif.php使用的模板文件如下:  
 elseif.html:    

<!-- IF usergroup="ADMIN" --> 
<a href="admin.php"> Admin Staff Login </a><br> 
<!-- ELSEIF usergroup="SUPPORT" --> 
<a href="support.php"> Support Staff Login </a><br> 
<!-- ELSEIF usergroup --> 
<a href="other.php"> Standard Login </a><br> 
<!-- ELSE --> 
You don't even have a usergroup! 
<!-- ENDIF -->



elseif.php执行效果如下:  
 输出:  

<a href="other.php"> Standard Login </a><br>



SmartPHP 例子: begin end 

begin ... end 结构提供了一种方法,使用数字索引数组来输出重复的相似的内容。数字索引数组的每一个元素,应该是一个散列数组,<!-- begin --> and <!-- end --> 标签类似一个小的模板,他分析内嵌的模板片断,并使用这个散列数组来生成内容。  

每个散列数组可以使用以下的两个扩展参数:  

ROWCNT :当前元素的在父数组之中的实际位置. (0,1,2,3,...n)  
ROWBIT : 表示ROWCNT的二进制字节的最后一位,也就是奇偶值. (0,1,0,1,0,1,...)  

begin ... end 块可以很容易的嵌套使用,他们会被自动的递归分析.  
 begin_end.php:  

<?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(); 
?> 



begin_end.php使用的模板如下:  
 begin_end.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>



begin_end.php的运行效果如下:  
 输出:  

<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>

多层嵌套

<!-- BEGIN result -->
<a href="./?v=art&op=list&id={c_id}">{c_title}</a>
    <!-- BEGIN list -->
    <li style="width:160px;float:left;"><a href="./?v=help&op=detail&id={a_id}">{a_title}</a></li>
    <!-- END list -->
<!-- END result -->

$result = $cat->getChildBySort('0,1,8,');

$num = count($result);
$condition = array('a_ispass'=>1, 'rows'=>SHOWLIST_PAGE_SIZE, 'artOrder'=>5);

for($i = 0; $i < $num; $i++) {
$condition['c_path'] = $result[$i]['absPath'];
$result[$i]['list'] = $art->listArticle($condition);
}
分享到:
评论
1 楼 naryCC 2013-05-07  
虽然没有smarty那么方便,但是由于我们的环境太老不得不使用smarttemplate, 这篇文章总结的挺好,基本上需要的功能都在里面,又是在其他搜索到的文章里面没有提到的{parent.title}用法

相关推荐

    smarttemplate_1.2.1_Demo

    对smarttemplate1.2.1 中的文件结构和部分程序代码稍做了一下修改,在IIS或Apache服务器上配置OK后可以直接运行浏览,不用改动任何代码,便于初学者学习研究。 此Demo已在本机测试通过 测试环境:WINDOWS2003+IIS6+...

    smartTemplate 简单中文手册

    smartTemplate 简单中文手册

    smartTemplate使用Demo

    smartTemplate使用举例。一个小demo

    [其他类别]SmartTemplate V1.2.1_smarttemplate-1.2.1.zip

    [其他类别]SmartTemplate V1.2.1_smarttemplate-1.2.1.zip

    SmartTemplate 1.2.1

    企业级PHP开发的模板引擎

    smarttemplate 应用案例并生成html

    smarttemplate 应用案例并生成htmlsmarttemplate 应用案例并生成htmlsmarttemplate 应用案例并生成html

    基于PHP模块引擎SmartTemplate高校图书管理系统.pdf

    基于PHP模块引擎SmartTemplate高校图书管理系统.pdf

    基于php+mysql+smarttemplate的图片共享系统设计与实现(源码+文档)_php_mysql图片共享系统.zip

    资源名字:基于php+mysql+smarttemplate的图片共享系统设计与实现(源码+文档)_php_mysql图片共享系统.zip 资源内容:项目全套源码+完整文档 源码说明: 全部项目源码都是经过测试校正后百分百成功运行。 适合场景...

    [其他类别]SmartTemplate V1.2.1_smarttemplate-1.2.1.rar

    【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。...【项目质量】:所有源码都经过严格测试,可以直接...

    SmartTemplate-开源

    QuickSkin(以前称为SmartTemplate)是一种PHP模板引擎/编译器,旨在支持大规模的基于Web的应用程序。 (HTML-)模板被转换为PHP代码并非常快速地执行。 http://sourceforge.net/projects/quickskin/

    O-Blog v2.5 Beta Build 0812 繁体中文版

    模板引擎采用 smarttemplate 6.时间显示更详细 7.安装程序自动建立数据库 9.日志置顶功能 10.发表评论不再需要写标题 11.日志和记事增加表情功能 12.优化的 RSS13.增加RSS1.014.分类可以放在横排...

    O-Blog v2.6 build 080705 简体中文正式版.rar

    5.模板引擎采用 smarttemplate 6.时间显示更详细 7.安装程序自动建立数据库 9.日志置顶功能 10.发表评论不再需要写标题 11.日志和记事增加表情功能 12.优化的 RSS 13.增加RSS1.0 14.分类可以放在横排也可以...

    冰山信息发布系统(IBArticle)

    [系统特点] 使用smartTemplate模板引擎,速度极快. 使用XHTML+CSS WEB标准技术,页面体积小,速度快. 文章和相册都支持无限的分类和子分类. 支持多套风格,用户也可自定义风格. (目前系统提供二套风格). 文章可进行...

    Quickskin

    PHP的Smarttemplate模板技术的最新版更名为Quickskin,是一个轻量级.轻巧,强大,高效的模板引擎,能很好地实现PHP的MVC开发模式

    Lightemplate (php模版类).rar

    可以包含扩展函数,整个模版文件没有外部include和require,减少io操作, 加载三个模版变量,两个数组变量,比smarttemplate快...单纯加载模版文件,不设置任何变量lightemplate平均0.3毫秒左右,smarttemplate至少要1毫秒.

    PHP企业网站源码,数据库是mysql

    PHP企业网站 PHP+mysqlphp+adodb+smarttemplate+access(mdb)带后台不限平台。屏蔽了调试的sql语句 解决了php5下的白屏问题 解决了删除后跳转的出错问题

Global site tag (gtag.js) - Google Analytics