(编辑:jimmy 日期: 2025/7/5 浏览:2)
本文实例讲述了PHP实现简单的模板引擎功能。分享给大家供大家参考,具体如下:
php web开发中广泛采取mvc的设计模式,controller传递给view层的数据,必须通过模板引擎才能解析出来。实现一个简单的仅仅包含if,foreach标签,解析$foo变量的模板引擎。
编写template模板类和compiler编译类。代码如下:
<"htmlcode"><"<", '<"<", '<"htmlcode">/** * [render 渲染模板文件] * @param [type] $file [待编译的文件] * @param [type] $values [键值对] * @param array $templateConfig [编译配置] * @return [type] [description] */ protected function render($file, $values, $templateConfig = []) { $di = Container::getInstance(); //依赖注入实例化对象 $di->template = function () use ($di, $templateConfig) { $di->compiler = 'foo\base\Compiler'; $compiler = $di->compiler; return new \foo\base\Template($compiler, $templateConfig); }; $di->template->assign($values)->show($file); }Container类如下:
<"htmlcode">public function offsetExists($offset) { return array_key_exists($offset, get_object_vars($this)); } public function offsetUnset($key) { if (array_key_exists($key, get_object_vars($this)) ) { unset($this->{$key}); } } public function offsetSet($offset, $value) { $this->{$offset} = $value; } public function offsetGet($var) { return $this->$var; }在某一控制器中就可以调用父类Controller的render方法啦
复制代码 代码如下:$this->render('test\index', ['name' => 'tom', 'age' => 20, 'friends' => ['jack', 'rose']], ['cacheTime' => 10]);编写视图模板文件'test\index':
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <p>展示模板文件视图</p> <p>{$name}</p> <p>{$age}</p> <"_blank" href="//www.jb51.net/Special/350.htm">PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》希望本文所述对大家PHP程序设计有所帮助。
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
最新资源