43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|||
|
|
/**
|
||
|
|
* 视图渲染。
|
||
|
|
*
|
||
|
|
* 流程:render() 把数据展开 → 把「本页模板文件路径 / 当前页标识 / 标题」交给
|
||
|
|
* layout.php → layout 在正文位置 require 本页模板。
|
||
|
|
*/
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App;
|
||
|
|
|
||
|
|
final class View
|
||
|
|
{
|
||
|
|
public static function render(string $tpl, array $data = [], string $page = ''): void
|
||
|
|
{
|
||
|
|
$file = dirname(__DIR__) . '/templates/' . $tpl . '.php';
|
||
|
|
if (!is_file($file)) {
|
||
|
|
throw new \RuntimeException('模板不存在:' . $tpl);
|
||
|
|
}
|
||
|
|
|
||
|
|
$tplFile = $file;
|
||
|
|
$pageKey = $page ?: $tpl;
|
||
|
|
$pageTitle = $data['title'] ?? '家书';
|
||
|
|
$extraHead = $data['extraHead'] ?? '';
|
||
|
|
|
||
|
|
extract($data, EXTR_SKIP);
|
||
|
|
require dirname(__DIR__) . '/templates/layout.php';
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 独立的整页(登录、安装、错误),不套顶栏 */
|
||
|
|
public static function renderBare(string $tpl, array $data = []): void
|
||
|
|
{
|
||
|
|
$file = dirname(__DIR__) . '/templates/' . $tpl . '.php';
|
||
|
|
if (!is_file($file)) {
|
||
|
|
throw new \RuntimeException('模板不存在:' . $tpl);
|
||
|
|
}
|
||
|
|
$pageTitle = $data['title'] ?? '家书';
|
||
|
|
extract($data, EXTR_SKIP);
|
||
|
|
require $file;
|
||
|
|
}
|
||
|
|
}
|