├── .gitignore ├── README.md ├── composer.json └── src └── Driver.php /.gitignore: -------------------------------------------------------------------------------- 1 | /nbproject/private/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | #think-angular 3 | 4 | > 仿angularjs的php模板引擎 5 | 6 | ## 使用说明 7 | 8 | 此模板引擎针对能够使用angularjs的php开发者编写, 主要特点是 不需要额外的标签定义, 全部使用属性定义, 写好模板文件在IDE中不会出现警告和错误, 格式化代码的时候很整洁, 因为套完的模板文件还是规范的html 9 | 10 | 注: 一个标签上可以使用多个模板指令, 指令有前后顺序要求, 所以要注意属性的顺序, 在单标签上使用模板属性时一定要使用/>结束, 如 <input php-if="$is_download" type="button" value="下载" />, <img php-if="$article['pic']" src="{$article.pic}" /> 等等, 具体可参考手册. 11 | 12 | ## 安装方法 13 | https://www.kancloud.cn/shuai/php-angular/151359 14 | 15 | ## 开发手册 16 | 看云文档托管平台: http://www.kancloud.cn/shuai/php-angular 17 | 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "topthink/think-angular", 3 | "description": "think angular view engine", 4 | "homepage": "http://kancloud.cn/shuai/php-angular", 5 | "license": "Apache-2.0", 6 | "authors": [ 7 | { 8 | "name": "玩具机器人", 9 | "email": "zhaishuaigan@qq.com" 10 | } 11 | ], 12 | "support": { 13 | "issues": "https://github.com/top-think/think-angular/issues" 14 | }, 15 | "require": { 16 | "php": ">=5.4.0", 17 | "php-angular/php-angular": "2.0.*" 18 | }, 19 | "autoload": { 20 | "classmap": { 21 | "think\\view\\driver\\Angular": "src/Driver.php" 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /src/Driver.php: -------------------------------------------------------------------------------- 1 | 9 | // +---------------------------------------------------------------------- 10 | 11 | namespace think\view\driver; 12 | 13 | use PHPAngular\Angular as AngularTpl; 14 | use think\App; 15 | use think\Request; 16 | use think\template\driver\File as Storage; 17 | 18 | class Angular 19 | { 20 | 21 | private $template = null; 22 | private $config = []; 23 | private $storage = null; 24 | 25 | public function __construct($config = []) 26 | { 27 | $default = [ 28 | 'debug' => App::$debug, // 是否开启调试模式 29 | 'tpl_path' => App::$modulePath . 'view' . DS, // 模板目录 30 | 'tpl_suffix' => '.html', // 模板后缀 31 | 'tpl_cache_path' => RUNTIME_PATH . 'temp' . DS, // 模板缓存目录 32 | 'tpl_cache_suffix' => '.php', // 模板缓存文件后缀 33 | 'directive_prefix' => 'php-', // 指令前缀 34 | 'directive_max' => 10000, // 指令的最大解析次数 35 | ]; 36 | 37 | $this->config = array_merge($default, $config); 38 | // 初始化模板编译存储器 39 | $this->storage = new Storage(); 40 | } 41 | 42 | /** 43 | * 配置模板引擎 44 | * @access private 45 | * @param string|array $name 参数名 46 | * @param mixed $value 参数值 47 | * @return void 48 | */ 49 | public function config($name, $value = null) 50 | { 51 | if (is_array($name)) { 52 | $this->config = array_merge($this->config, $name); 53 | } elseif (is_null($value)) { 54 | return isset($this->config[$name]) ? $this->config[$name] : null; 55 | } else { 56 | $this->config[$name] = $value; 57 | } 58 | } 59 | 60 | /** 61 | * 获取模版运行结果 62 | * @param string $template 模版地址 63 | * @param array $data 模版数据 64 | * @param array $config 配置 65 | * @return string 66 | */ 67 | public function fetch($template, $data = [], $config = []) 68 | { 69 | $this->template = new AngularTpl($this->config); 70 | // 处理模版地址 71 | $template = $this->parseTemplatePath($template); 72 | $request = Request::instance(); 73 | $module = $request->module() ?: 'default'; 74 | // 根据模版文件名定位缓存文件 75 | $tpl_cache_file = $this->config['tpl_cache_path'] . 'angular/' . $module . '/' . md5($template) . '.php'; 76 | if ($this->config['debug'] || !is_file($tpl_cache_file) || !$this->storage->check($tpl_cache_file, 0)) { 77 | // 编译模板内容 78 | $content = $this->template->compiler($template, $data); 79 | $this->storage->write($tpl_cache_file, $content); 80 | } 81 | $this->storage->read($tpl_cache_file, $data); 82 | } 83 | 84 | /** 85 | * fetch的别名 86 | * @param string $template 模版地址 87 | * @param array $data 模版数据 88 | * @param array $config 配置 89 | * @return string 90 | */ 91 | public function display($template, $data = [], $config = []) 92 | { 93 | return $this->fetch($template, $data, $config); 94 | } 95 | 96 | /** 97 | * 如果模版为空, 则通过URL参数获取模版地址 98 | * @param string $template 模版地址 99 | * @return string 100 | */ 101 | public function parseTemplatePath($template = '') 102 | { 103 | $request = Request::instance(); 104 | $controller = strtolower($request->controller()); 105 | $action = $request->action(); 106 | if (!$template) { 107 | // 没有传模版名 108 | $template = $controller . DS . $action; 109 | $template = str_replace('.', DS, $template); 110 | return $template; 111 | } elseif (strpos($template, '/') === false) { 112 | // 只传了操作名 113 | $template = $controller . DS . $template; 114 | $template = str_replace('.', DS, $template); 115 | return $template; 116 | } 117 | // 默认原样返回 118 | return $template; 119 | } 120 | 121 | public function __call($method, $params) 122 | { 123 | return call_user_func_array([$this->template, $method], $params); 124 | } 125 | 126 | } 127 | --------------------------------------------------------------------------------