public static function render($file, $data = null)
{
$file_path = dirname(__DIR__) . "/views/{$file}.php";
// 如果给定文件名不存在或不可读
if (!is_readable($file_path)) {
throw new \Exception("找不到模板文件:{$file_path}。");
}
// 如果是数组就合并
if (is_array($data)) {
array_merge($data);
}
extract($data);
include $file_path;
}
然后模板中这样使用:
$a = 1;
View::render('home/index', compact('a'));
我不清楚该如何检查是否有安全问题 🥺,小问题就是同名的变量 extract 会冲突。