md / pug-slim
该软件包最新版本(1.0)没有可用的许可信息。
1.0
2017-06-25 12:58 UTC
Requires
- psr/http-message: ^1.0
- pug-php/pug: ^2.6
This package is not auto-updated.
Last update: 2024-09-15 02:58:00 UTC
README
这是一个渲染 Pug 视图脚本为 PSR-7 响应对象的渲染器(高度基于 PhpRenderer)。它与 Slim 框架 3 协作良好。
安全风险
Pug Renderer 在内部使用 Pug.php。Pug Renderer 仅是 Pug.php 用于与 Slim 3 一起工作的薄层。我们将不会跟踪漏洞。请参考原始项目以了解更多如何防止问题和报告漏洞。
安装
使用 Composer 安装
$ composer require md/pug-slim
与 Slim 3 一起使用
use Slim\Views\PugRenderer; include "vendor/autoload.php"; $app = new Slim\App(); $container = $app->getContainer(); $container['renderer'] = new PugRenderer("./templates"); $app->get('/hello/{name}', function ($request, $response, $args) { return $this->renderer->render($response, "/hello.pug", $args); }); $app->run();
与任何 PSR-7 项目一起使用
//Construct the View $pugView = new PugRenderer("./path/to/templates"); //Render a Template $response = $pugView->render(new Response(), "/path/to/template.pug", $yourData);
模板变量
您可以为您的渲染器添加变量,这些变量将可用于您渲染的所有模板。
// via the constructor $templateVariables = [ "title" => "Title" ]; $pugView = new PugRenderer("./path/to/templates", $templateVariables); // or setter $pugView->setAttributes($templateVariables); // or individually $pugView->addAttribute($key, $value);
通过 ->render()
传入的数据将优先于属性。
$templateVariables = [ "title" => "Title" ]; $pugView = new PhpRenderer("./path/to/templates", $templateVariables); //... $pugView->render($response, $template, [ "title" => "My Title" ]); // In the view above, the $title will be "My Title" and not "Title"
异常
\RuntimeException
- 如果模板不存在
\InvalidArgumentException
- 如果 $data 包含 'template'
参考资料
- 要了解更多关于 Pug 的信息,请访问 https://pug.node.org.cn
- 这是一个在线的 HTML 到 Pug 转换器 https://html2pug.herokuapp.com/