SmartTemplate(适用于企业级PHP开发的模板引擎)

    技术2025-06-08  32

    看到一个好文章,共享给大家 作者:乔楚 基本方法 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> 原文 http://www.cnblogs.com/whale/archive/2007/05/31/766710.html
    最新回复(0)