当前位置:Gxlcms > PHP教程 > php模板引擎技术简单实现_PHP

php模板引擎技术简单实现_PHP

时间:2021-07-01 10:21:17 帮助过:26人阅读

用了smarty,tp过后,也想了解了解其模板技术是怎么实现,于是写一个简单的模板类,大致就是读取模板文件->替换模板文件的内容->保存或者静态化

tpl.class.php主要解析

  assign 方法实现

  1. /**
  2. * 模板赋值操作
  3. * @param mixed $tpl_var 如果是字符串,就作为数组索引,如果是数组,就循环赋值
  4. * @param mixed $tpl_value 当$tpl_var为string时的值,默认为 null
  5. */
  6. public function assign($tpl_var,$tpl_value=null){
  7. if(is_array($tpl_var) && count($tpl_var) > 0){
  8. foreach ($tpl_var as $k => $v) {
  9. $this->tpl_vars[$k] = $v;
  10. }
  11. }elseif($tpl_var){
  12. $this->tpl_vars[$tpl_var] = $tpl_value;
  13. }
  14. }

fetch 方法实现

  1. /**
  2. * 生成编译文件
  3. * @param string $tplFile 模板路径
  4. * @param string $comFile 编译路径
  5. * @return string
  6. */
  7. private function fetch($tplFile,$comFile){
  8. //判断编译文件是否需要重新生成(编译文件是否存在或者模板文件修改时间大于编译文件的修改时间)
  9. if(!file_exists($comFile) || filemtime($tplFile) > filemtime($comFile)){
  10. //编译,此处也可以使用ob_start()进行静态化
  11. $content = $this->tplReplace(file_get_contents($tplFile));
  12. file_put_contents($comFile, $content);
  13. }
  14. }

简单编译方法:按照规则进行正则替换

  1. /**
  2. * 编译文件
  3. * @param string $content 待编译的内容
  4. * @return string
  5. */
  6. private function tplReplace($content){
  7. //转义左右定界符 正则表达式字符
  8. $left = preg_quote($this->left_delimiter,'/');
  9. $right = preg_quote($this->right_delimiter,'/');
  10. //简单模拟编译 变量
  11. $pattern = array(
  12. //例如{$test}
  13. '/'.$left.'\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)'.$right.'/i'
  14. );
  15. $replace = array(
  16. '<?php echo $this->tpl_vars[\'${1}\']; ?>'
  17. );
  18. //正则处理
  19. return preg_replace($pattern, $replace, $content);
  20. }

display = fetch+echo

  1. /**
  2. *
输出内容 * @param string $fileName 模板文件名 */ public function display($fileName){ //模板路径 $tplFile = $this->template_dir.'/'.$fileName; //判断模板是否存在 if(!file_exists($tplFile)){ $this->errorMessage = '模板文件不存在'; return false; } //编译后的文件 $comFile = $this->compile_dir.'/'.md5($fileName).'.php'; $this->fetch($tplFile,$comFile);        include $comFile; }

其他属性

  1. //模板文件存放位置
  2. private $template_dir = 'templates';
  3. //编译文件存放位置
  4. private $compile_dir = 'compiles';
  5. //左定界符
  6. private $left_delimiter = '{';
  7. //右定界符
  8. private $right_delimiter = '}';
  9. //内部临时变量,存储用户赋值
  10. private $tpl_vars = array();
  11. //错误信息
  12. private $errorMessage = '';
  13. /**
  14. * 修改类属性的值
  15. * @param array $configs 需要修改的相关属性及值
  16. * @return bool
  17. */
  18. public function setConfigs(array $configs){
  19. if(count($configs) > 0){
  20. foreach ($configs as $k => $v) {
  21. if(isset($this->$k))
  22. $this->$k = $v;
  23. }
  24. return true;
  25. }
  26. return false;
  27. }

测试

模板文件 testTpl.html

  1. <meta charset="UTF-8">
  2. <title>test_tpl_demo</title>
  3. {$name}:{$age}:{$message}
  4. 运行文件 test_tpl.php
  5. <?php
  6. require 'Tpl.class.php';
  7. $tpl = new Tpl();
  8. $tplarr = array(
  9. 'name'=>'waited',
  10. 'age'=>'100'
  11. );
  12. $tpl->assign($tplarr);
  13. $tpl->assign('message','this is a demo');
  14. $tpl->display('testTpl.html');
  15. ?>

输出:waited:100:this is a demo

生成编译文件:972fa4d270e295005c36c1dbc7e6a56c.php

以上就是本文的全部内容,希望对大家的学习有所帮助。

人气教程排行