Repository navigation
Expand file tree
/
Copy pathView.php
More file actions
55 lines (51 loc) · 1.96 KB
/
Copy pathView.php
File metadata and controls
55 lines (51 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace Loom;
/**
* 在隔离的输出缓冲区里执行 PHP 模板。模板只能通过 $data 读取传入的值,输出不会自动转义。
*
* 构造参数:无,本类型未声明构造参数。
*
* 返回值:使用 new 构造时得到 View 实例。
*
* 使用示例(依赖对象由所在应用或测试环境提供):
* @example $html = View::render($templatePath, ['title' => '示例']);
*/
final class View
{
/**
* 在新的输出缓冲区中 require 模板,数据只通过 $data 传入,输出不做 HTML 转义。文件不存在,或执行后缓冲区层级不是进入时加一层,抛出 LogicException。异常时清掉多开的缓冲区;成功时返回这一层缓冲的内容。
*
* @param string $template 要执行的 PHP 模板文件路径。
* @param array $data 模板通过 $data 读取的值。默认空数组。
* @return string 模板写出的正文。
*
* 使用示例(依赖对象由所在应用或测试环境提供):
* @example $html = View::render($templatePath, ['title' => '示例']);
*/
public static function render(string $template, array $data = []): string
{
if (!is_file($template)) {
throw new \LogicException('Template does not exist: ' . $template);
}
$level = ob_get_level();
ob_start();
try {
$render = static function (string $template, array $data): void {
require $template;
};
$render($template, $data);
if (ob_get_level() !== $level + 1) {
throw new \LogicException('Template changed output buffer ownership');
}
return ob_get_clean();
} finally {
while (ob_get_level() > $level) {
if (!ob_end_clean()) {
break;
}
}
}
}
}
// 传输和连接方法由 Origami 标准库提供。