phpolar/php-templating

此包已被废弃,不再维护。作者建议使用phpolar/pure-php包。

纯PHP模板


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文档

返回顶部