smarty核心思想 自制模板引擎
2017-03-01 20:38
253 查看
<?php $tit = '今天下雨了,淋了半条街'; function tit($file){ //读文件 $h = file_get_contents($file); $h = str_replace('{$','<?php echo $',$h); $h = str_replace('}',';?>',$h); //新文件名 1.html.php $tmp = $file.'.php'; //写到一个文件里 file_put_contents($tmp,$h); return $tmp; } include(tit('1.html'));
读取1.html然后把{$tit} 替换成php语法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {$tit} </body> </html>
封装成一个类,用面向对象思想来搞
<?php class Mini { public $data = array(); public function tit($file) { //读文件 $h = file_get_contents($file); $h = str_replace('{$', '<?php echo $this->data[\'', $h); $h = str_replace('}', '\'];?>', $h); //新文件名 1.html.php $tmp = $file . '.php'; //写到一个文件里 file_put_contents($tmp, $h); return $tmp; } public function assign($key,$value){ $this->data[$key] = $value; } public function display($file){ $filetemp = $this->tit($file); include($filetemp); //这个时候没有这个变量 //echo $tit; } } $tit = '今天下雨了,淋了半条街'; $mini = new Mini(); $mini->assign('mytit',$tit); $mini->display('1.html');
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {$mytit} </body> </html>
相关文章推荐
- 模板引擎-- PHP模板引擎【smarty】
- smarty模板引擎_3-预保留变量
- 【PHP web 2.0 开发实践】2.9 Smarty 模板引擎
- 模板引擎SMARTY
- 使用模板引擎 (smarty 2.6.18)
- [自制模板引擎]写一个迷你模板引擎
- PHP模板引擎 Smarty 程序设计部分 基础说明
- PHP 模板引擎 Smarty
- Smarty模板技术/引擎——变量操作(2)
- 浅谈框架zend framework和模板引擎smarty的区别【转载】
- smarty自定义引擎模板
- 模板引擎SMARTY
- 模板引擎(smarty)知识点总结五
- smarty模板引擎_7-自定义函数
- 模板引擎SMARTY
- 并查集(kruskal的核心思想)模板
- [自制模板引擎] 为模板引擎增加解析表达式功能
- Yaf 结合用户自定义的视图(模板)引擎Smarty(Yaf + Smarty)
- 类似于smarty之类的模板引擎的模板引擎原型
- 在 CodeIgniter 2中集成 Smarty 3 模板引擎