midorikocak / view
View是MVC中的V。一个用于包含在MVC项目中的小型库。
Requires
- php: ~7.4
- ext-json: *
- eftec/bladeone: ^3.34
Requires (Dev)
- guzzlehttp/guzzle: ^6.5
- opsway/psr12-strict-coding-standard: ^0.3.0
- phpunit/phpunit: >=8.5
- squizlabs/php_codesniffer: ^3.5
- symfony/process: ^5.0
This package is not auto-updated.
Last update: 2024-09-21 02:12:38 UTC
README
View是MVC中的V。一个用于包含在MVC项目中的小型库。它允许您将表示层与应用程序的其他部分分离。
注意:检查用户输入是您的责任。此库仅渲染模板。没有其他功能。
安装
通过Composer
$ composer require midorikocak/view
用法
假设您有如下博客文章数据
$articleData = [ 'title' => 'Writing object oriented software is an art.', 'sections' => [ [ 'title' => 'Introduction', 'content' => [ 'Some people say that you have to be genius, or man, but they mostly does not undertsand what is humble programming. Programming should be a modest person\'s craft.', 'Generally those people does exclude others, create clickbait articles, they make you feel less confident.', 'Do not care about them and just try to create things. Learn. Be hungry.', ], ], ], 'created' => '2020-01-25 00:00:00', ];
并且您想使用如下模板渲染文章
// template.php <?php if (isset($created) && $created !== '') { $datetime = strtotime($created); $formattedDate = date('D, j Y', $datetime); } ?> <article> <?php if ($title !== '') : ?> <header> <h2><?= $title ?></h2> </header> <?php endif; ?> <?php foreach ($sections as $section) : ?> <section> <?php if (isset($section['title'])) : ?> <h3><?= $section['title'] ?></h3> <?php endif; ?> <?php foreach ($section['content'] as $paragraph) : ?> <p><?= $paragraph ?></p> <?php endforeach; ?> </section> <?php endforeach; ?> <?php if ($created !== '') : ?> <footer> <time datetime="<?= $datetime ?>"> <?= $formattedDate ?> </time> </footer> <?php endif; ?> </article>
您可以使用默认的FileRenderer提取带有变量的PHP文件,并且可以使用带有变量的普通PHP文件进行渲染
$renderer = new FileRenderer(); $view = new View($this->renderer); $view->setTemplate('template.php'); echo $this->view->render($this->articleData);
这将输出如下渲染的HTML,变量被渲染
<article> <header> <h2>Writing object oriented software is an art.</h2> </header> <section> <h3>Introduction</h3> <p>Some people say that you have to be genius, or man, but they mostly does not undertsand what is humble programming. Programming should be a modest person's craft.</p> <p>Generally those people does exclude others, create clickbait articles, they make you feel less confident.</p> <p>Do not care about them and just try to create things. Learn. Be hungry.</p> </section> <footer> <time datetime="1579910400">Sat, 25 2020</time> </footer> </article>
注意:将变量提取到运行时可能不安全。
普通模板
如果您有带有占位符的普通模板,例如{{ someVariable }}
,您可以使用TemplateRenderer
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{{ title }}</title> </head> <body> <article> <header> <h2>{{ title }}</h2> </header> <section> <h3>{{ sectionTitle }}</h3> <p>{{ paragraph }}</p> </section> <footer> <time datetime="{{ created }}"> {{ formatted }} </time> </footer> </article> </body> </html>
要使用一些数据渲染此模板
$templateRenderer = new TemplateRenderer(); $view = new View($templateRenderer); $view->setTemplate('tests/View/plain.template.html'); $this->assertNotEmpty($view->render( [ 'title' => 'Object Oriented Programming', 'sectionTitle' => 'Introduction', 'parapgraph' => 'Writing object oriented software is an art.', 'created' => '2020-01-25 00:00:00', 'formatted' => 'Mon 25, 2020', ] ));
这将输出如下渲染的HTML,变量被渲染
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Object Oriented Programming</title> </head> <body> <article> <header> <h2>Object Oriented Programming</h2> </header> <section> <h3>Introduction</h3> <p></p> </section> <footer> <time datetime="2020-01-25 00:00:00"> Mon 25, 2020 </time> </footer> </article> </body> </html>
使用Blade模板
要使用Blade模板,请使用包含的BladeRenderer
。
$bladeRenderer = new BladeRenderer('tests/View', '/tmp'); $view = new View($bladeRenderer); $view->setTemplate('app'); echo $view->render( [ 'title' => 'OOP', 'content' => 'Writing object oriented software is an art.', ] );
自定义渲染器
要创建自己的渲染器或使用另一个渲染器库扩展库,请实现RendererInterface
<?php declare(strict_types=1); namespace midorikocak\view; interface RendererInterface { public function render(string $template, array $data): string; }
渲染数据对象
如果您有实现toArray()
方法的数据对象,您可以使用ViewableTrait
与它们一起使用。因此,在您的控制器中,您可以编写如下代码
$article->template = 'tests/View/posts/post.php'; echo $article->render();
数据对象中的自定义查看器
您可以通过使用自定义渲染器初始化的查看器来访问数据对象,从而更改数据对象的查看器对象。
$templateRenderer = new TemplateRenderer(); $view = new View($templateRenderer); $article->view = $view; $article->template = 'tests/View/plain.template.html'; echo $article->render();
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
测试
$ composer test
贡献
有关详细信息,请参阅CONTRIBUTING和CODE_OF_CONDUCT。
安全
如果您发现任何与安全相关的问题,请通过电子邮件mtkocak@gmail.com报告,而不是使用问题跟踪器。
致谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。