Files
FamilyBook/app/View.php
T

51 lines
1.7 KiB
PHP
Raw Normal View History

<?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';
}
/**
* 独立的整页(登录、安装、错误),不套顶栏 —— 但**仍然要是一份完整 HTML**。
*
* ⚠️ 曾经这里直接 require $file,输出里没有 doctype / head / CSS,
* 浏览器拿默认样式渲染,登录页看起来就像「完全没样式」。
* 现在统一套一层 layout-bare.php(无顶栏的最小骨架)。
*/
public static function renderBare(string $tpl, array $data = []): void
{
$file = dirname(__DIR__) . '/templates/' . $tpl . '.php';
if (!is_file($file)) {
throw new \RuntimeException('模板不存在:' . $tpl);
}
$tplFile = $file;
$pageTitle = $data['title'] ?? '家书';
$extraHead = $data['extraHead'] ?? '';
extract($data, EXTR_SKIP);
require dirname(__DIR__) . '/templates/layout-bare.php';
}
}