phpolar/pure-php

纯PHP模板

2.0.0 2023-09-02 23:44 UTC

This package is auto-updated.

Last update: 2024-08-27 05:00:25 UTC


README

Phpolar Logo

纯PHP

仅使用PHP的模板。真的就是这样。

支持使用纯PHP模板,具有自动XSS缓解功能。

Coverage Status Version PHP Version Require License Total Downloads

目录

  1. 安装
  2. 使用方法
  3. 示例
  4. API文档

安装

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;
    }
}

API文档

返回顶部