您当前的位置: 首页 > 网站编程 > PHP教程 > php Smarty模板引擎 foreach循环

php Smarty模板引擎 foreach循环

作者:不详 来源:网络 发布时间: 2014-08-10 21:55 点击:
模板文件: 代码如下 !DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd html head meta http-equiv=Content-Type content=text/html; charset=UTF-8 title{$web_tile}/title /head body {$article_title} br/ -- by

php Smarty模板引擎 foreach循环

  模板文件:

  

  

  

  

  

  

  

  

  

  

  
代码如下


  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  <html>

  <head>

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  <title>{$web_tile}</title>

  </head>

  <body>

  {$article_title}

  <br/>

    -- by {$author}

  <hr/>

  <br/>

  {$content}

  <br/>

  -- publish @ {$time}

  <br/>

  <br/>

  foreach test:

  {foreach ( from=url key=b item=c )}

  <a href="index.php?artcle_id={==b}">{==c}</a>

  {/foreach}

  <br/>

  </body>

  </html>

  


  解析引擎:

  

  

  

  

  

  

  

  

  

  

  
代码如下
// var

    $pattern_var = "/{$left_tag}$([wd]+){$right_tag}/";

    $replace_var = '<?php echo $this->var_tpl_arr["$1"];?>';

   

    if (

preg_match($pattern_var, $content)) {

     $content = preg_replace($pattern_var, $replace_var, $content);

    }

   

    // foreach

    preg_match_all("/{$left_tag}foreachs+([^{]+?){$right_tag}/is", $content, $match_foreach);

    if (isset($match_foreach[1]) && is_array($match_foreach)) {

     foreach($match_foreach[1] as $match_key => $match_value) {

      $split_foreachs = array_filter(preg_split('/s+/is', $match_value));

      $new_foreach_tag = array();

      foreach($split_foreachs as $split_foreach) {

       $split = explode("=", $split_foreach);

       if (count($split == 2)) {

        if(in_array($split[0], array("from","item","key"))) {

  //过滤标签 不存在过滤

         $new_foreach_tag[$split[0]] = $split[1];

        }

       }

      }

     

      $from = $key = $item = '';

      extract($new_foreach_tag);

      $key = ($key) ? '$'.$key.' =>' : '' ;

      $replace_foreach = '<?php foreach($this->var_tpl_arr["'.$from.'"] as '.$key.' $'.$item.') { ?>';

      $content = str_replace($match_foreach[0][$match_key], $replace_foreach, $content);

     

     }

    }

   

    $pattern_foreach = "/{$left_tag}/foreach{$right_tag}/";

    $replace_foreach = "<?php } ?>";

    if (preg_match($pattern_foreach, $content)) {

     $content = preg_replace($pattern_foreach, $replace_foreach, $content);

    }

   

    // var in statement

    $pattern_var = "/{$left_tag}==([wd]+){$right_tag}/";

    $replace_var = '<?php echo $$1;?>';

   

    if (preg_match($pattern_var, $content)) {

     $content = preg_replace($pattern_var, $replace_var, $content);

    }


  解析后:

  

  

  

  

  

  

  

  

  

  

  
代码如下


  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  <html>

  <head>

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  <title><?php echo $this->var_tpl_arr["web_tile"];?></title>

  </head>

  <body>

  <?php echo $this->var_tpl_arr["article_title"];?>

  <br/>

    -- by <?php echo $this->var_tpl_arr["author"];?>

  <hr/>

  <br/>

  <?php echo $this->var_tpl_arr["content"];?>

  <br/>

  -- publish @ <?php echo $this->var_tpl_arr["time"];?>

  <br/>

  <br/>

  foreach test:

  <?php foreach($this->var_tpl_arr["url"] as $b => $c) { ?>

  <a href="index.php?artcle_id=<?php echo $b;?>"><?php echo $c;?></a>

  <?php } ?>

  <br/>

  </body>

  </html>

  


  使用:

  

  

  

  

  

  

  

  

  

  

  
代码如下


  <?php

  require_once 'core/YATP.class.php';

  $app = new YATP();

  date_default_timezone_set("Asia/Shanghai");

  $app->is_cache = false;

  $article_title = "yet,it is a simple template engine";

  $author = "sanwhiteyu@tencent.com";

  $web_tile = "just test ";

  $content = "It is easy to write a simple template engine for yourself,what u can do is try to do it!";

  $time = date("Y-m-d H:i:s",time());

  $url = array(

    "url1"=>http://www.111cn.net,

    "url2"=>"http://www.baidu.com",

  );

  $app->assign("article_title",$article_title);

  $app->assign("author",$author);

  $app->assign("web_tile",$web_tile);

  $app->assign("content",$content);

  $app->assign("time",$time);

  $app->assign("url",$url);

  $app->display("index.html");

  


  // end of script效果:

  
分享到:
本文"php Smarty模板引擎 foreach循环"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 广告合作 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4