phpolar / pure-php
纯PHP模板
2.0.0
2023-09-02 23:44 UTC
Requires
- php: >= 8.1
Requires (Dev)
- ext-mbstring: *
- phan/phan: ^5.4
- php-coveralls/php-coveralls: ^2.5
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.7
README
纯PHP
仅使用PHP的模板。真的就是这样。
支持使用纯PHP模板,具有自动XSS缓解功能。
目录
安装
composer require phpolar/pure-php
使用方法
$page = new Page(); $safeContext = new HtmlSafeContext($page); $templateEng->render("path/to/template.php", $safeContext); // or... echo $templateEng->apply("path/to/template", $safeContext /* optional */);
仅模板基本名称
// or... echo $templateEng->apply("template", $safeContext /* optional */); // or... echo $templateEng->apply("template", $safeContext /* optional */);
模板引擎将在当前工作目录的相对路径下的
src/templates
目录中查找具有 .php, .phtml 或 .html 扩展名的文件。
示例 1
纯PHP模板
// template.php <!DOCTYPE html> <?php /** * @var Page $view */ $view = $this; ?> <html> <head> <style> body { font-family: <?= $view->font ?>; padding: 0; margin: 0; } form th { text-align: right; } form td { text-align: left; } .container { background-color: <?= $view->backgroundColor ?>; padding: 20px 0 90px } </style> </head> <body style="text-align:center"> <h1><?= $view->title ?></h1> <div class="container"> </div> </body> </html>
// Page.php class Page { public string $title; public string $backgroundColor = "#fff"; public string $font = "Arial"; public function __construct(string $title) { $this->title = $title; } }