在thinphp5中发现一个好用的模版引擎—think-angular,
此模板引擎主要特点是 不需要额外的标签定义, 全部使用属性定义, 写好的模板文件在IDE格式化代码的时候很整洁, 因为套完的模板文件还是规范的html,
注: 一个标签上可以使用多个模板指令, 指令有前后顺序要求, 所以要注意指令的顺序,
项目地址:https://github.com/top-think/think-angular
文档地址:https://www.kancloud.cn/shuai/php-angular/127397
index.php
<?php use thinkangularAngular; require '../src/Angular.php'; // 配置 $config = [ 'debug' => true, // 是否开启调试, 开启调试会实时生成缓存 'tpl_path' => './view/', // 模板根目录 'tpl_suffix' => '.html', // 模板的后缀 'tpl_cache_path' => './cache/', // 模板缓存目录 'tpl_cache_suffix' => '.php', // 模板缓存后缀 'directive_prefix' => 'php-', // 指令前缀 'directive_max' => 10000, // 指令的最大解析次数 ]; // 实例化 $view = new Angular($config); // 数据 $data = array( 'title' => 'Hello PHP Angular', 'list' => array( array('name' => 'name_1', 'email' => 'email_1@qq.com'), array('name' => 'name_2', 'email' => 'email_2@qq.com'), array('name' => 'name_3', 'email' => 'email_3@qq.com'), array('name' => 'name_4', 'email' => 'email_4@qq.com'), array('name' => 'name_5', 'email' => 'email_5@qq.com'), ), ); // 向模板引擎设置数据 $view->assign($data); // 输出解析结果 $view->display('index'); // 获取编译解析html结果 $html = $view->fetch('index'); // 获取模板编译后的php代码 $php = $view->compiler('index');
common/base.html
<!DOCTYPE html> <html> <head> <title>base</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div php-include="common/head"></div> <div php-block="left">这里是left, 会被替换</div> <div php-block="main">这里是main, 会被替换</div> </body> </html>
common/head.html
<div>这里是head</div>
common/index.html
<div php-extends="common/base"></div> <div php-block="left"> <div class="menu"> <ul php-function="echo_ul($list)"> <li php-foreach="$list as $cate"><a href="/category/{$cate.name}">{$cate.title}</a> <ul php-if="isset($cate['_child'])" php-call="echo_ul($cate['_child'])"></ul> </li> </ul> <ul php-call="echo_ul($list)"></ul> </div> </div> <div php-block="main"> <div class="name" php-init="$name='shuai'">{$name}</div> <div php-if="$is_download"><input type="button" onclick="down()"/></div> <div php-switch="$status"> <span php-case="1"> 正常 </span> <span php-default=""> 未知状态 </span> </div> <a php-for="$i = $page_start; $i <= $page_end; $i++" href="/article/lists?p={$i}">第{$i}页</a> <tr php-foreach="$list as $article"> <td>{$article.title}</td> <td>{:time_format($article['create_time'])}</td> <td> <a href="/article/detail?id={$article.id}">查看</a> <a href="/article/del?id={$article.id}">删除</a> </td> </tr> </div>