最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php Smarty模板引擎 foreach循环
时间:2022-06-25 02:18:36 编辑:袖梨 来源:一聚教程网
模板文件:
代码如下 | 复制代码 |
{$article_title} -- by {$author} {$content} -- publish @ {$time}
{foreach ( from=url key=b item=c )}
|
解析引擎:
代码如下 | 复制代码 |
// var $pattern_var = "/{$left_tag}$([wd]+){$right_tag}/"; $replace_var = '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 = '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 = ""; 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 = ''; if (preg_match($pattern_var, $content)) { $content = preg_replace($pattern_var, $replace_var, $content); } |
解析后:
使用:
代码如下 | 复制代码 |
require_once 'core/YATP.class.php'; $app = new YATP(); $article_title = "yet,it is a simple template engine"; $url = array( $app->assign("article_title",$article_title); |
// end of script效果:
相关文章
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21
- 《潜行者2:切尔诺贝利之心》使用手电筒方法介绍 11-21