chiron / php-renderer
此包已被废弃且不再维护。未建议替代包。
将 PHP 视图脚本渲染成 PSR-7 响应对象。
1.9.2
2020-08-09 13:38 UTC
Requires
- php: ^7.1
- chiron/template: ^1.6
- psr/container: ^1.0
Requires (Dev)
- phpstan/phpstan: ^0.9.2
- phpunit/phpunit: ^7.0
README
PHP Renderer
这是一个将 PHP 视图脚本渲染成 PSR-7 响应对象的渲染器。它与 Chiron 框架配合良好。
跨站脚本(XSS)风险
请注意,PHP-View 没有内置的 XSS 攻击缓解。使用 htmlspecialchars()
或类似 zend-escaper 的组件是开发者的责任。或者,可以考虑使用 Twig-View。
模板
您可以在 PHP 模板内部使用 $this
。 $this
将是实际的 PhpRenderer 对象,这将允许您渲染子模板。
安装
使用 Composer 安装
composer require chiron/php-renderer
与 Chiron 一起使用
use Chiron\Views\PhpRenderer; include "vendor/autoload.php"; $app = new Chiron\App(); $container = $app->getContainer(); $container['renderer'] = new PhpRenderer("./templates"); $app->get('/hello/{name}', function ($request, $response, $args) use ($container) { $text = $container->get('renderer')->render("/hello.php", $args); return $response->write($text); }); $app->run();
与任何 PSR-7 项目一起使用
//Construct the View $phpView = new PhpRenderer("./path/to/templates"); //Render a Template $text = $phpView->render("/path/to/template.php", $yourData); $response = $response->write($text);
模板变量
您现在可以向渲染器添加变量,这些变量将可用于您渲染的所有模板。
// via the constructor $templateVariables = [ "title" => "Title" ]; $phpView = new PhpRenderer("./path/to/templates", $templateVariables); // or setter $phpView->setAttributes($templateVariables); // or individually $phpView->addAttribute($key, $value);
通过 ->render()
传递的数据优先于属性。
$templateVariables = [ "title" => "Title" ]; $phpView = new PhpRenderer("./path/to/templates", $templateVariables); //... $phpView->render($template, [ "title" => "My Title" ]); // In the view above, the $title will be "My Title" and not "Title"
异常
\RuntimeException
- 如果模板不存在