wpcong123 2014-09-12 07:10
浏览 724

php仿smarty的引擎模板走不起来

templates/intro.tpl


{$title}
{$content}

intro.php

<?php
require_once("MyMinSmarty.class.php");
$mysmarty = new MyMinSmarty;

$mysmarty->assgin("title","我的第一个title");
$mysmarty->assgin("content","我的第一个content");
$mysmarty->display("intro.tpl");
?>

MyMinSmarty.class.php

<?php
class MyMinSmarty{
//定义模板路径
var $template_dir = "./templates/";
//定义编译文件路径
var $complie_dir = "./templates_c/";
//定义存储变量
var $tpl_vars = array();
function assgin($tpl_var,$val=num){//创建一个分配数据的方法,$tpl_var是变量,$val是要分配的值
if($tpl_var!=""){
$this->tpl_vars['$tpl_var']=$val;
}
return 0;}
function display($tpl_file){//创建一个显示数据的方法
$tpl_file_path=$this->template_dir.$tpl_file;//组合模板文件的路径
$complie_file_path=$this->complie_dir."com_".$tpl_file.".php";//组合编译文件的路径

   if(!file_exists($tpl_file_path)){//判断模板文件是否存在
      echo "文件不存在";
      return false;
   }
   $fpl_file_con=file_get_contents($tpl_file_path);//获取模板文件信息
      //echo "文件存在";


   $pattern = array(//正则
      '/{\s*\$([a-zA-Z_][a-zA-Z0-9_]*)\s*}/i'
    );
    $replace = array(//要替换的内容
      '<?php echo $this->tpl_vars["${1}"] ?>'
    );
    $new_str = preg_replace($pattern,$replace,$fpl_file_con);
    echo $new_str;
    file_put_contents($complie_file_path,$new_str);

    include $complie_file_path;


}

}

?>

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 链接问题 C++LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接