悠悠楠杉
网站页面
1.php
<?php
class Mini
{
protected $data = array();
//把外界变量放进data数组
public function assign($k, $v)
{
$this->data[$k] = $v;
}
public function comp($temp)
{
$html = file_get_contents($temp);
//注意 下行引用自身的data属性
$html = str_replace('{$', "<?php echo \$this->data['", $html);
$html = str_replace('}', "'];?>", $html);
$compfile = $temp . '.php';
file_put_contents($compfile, $html);
return $compfile;
}
public function display($temp)
{
$compfile = $this->comp($temp);
require $compfile;
}
}
$sql = "select * from art";
$title = '今天不下雨,ε=(´ο`*)))';
$mini = new Mini();
$mini->assign('title', $title);
$mini->display('1.html');
1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p>{$title}</p>
</body>
</html>