xtompie / tpl
1.0.0
2024-09-07 13:28 UTC
This package is auto-updated.
Last update: 2024-09-07 17:07:55 UTC
README
Tpl原生PHP模板系统。
简单、轻量级、无依赖、低抽象、低功能、易于扩展。
<?php use Xtompie\Tpl\Tpl; $tpl = new Tpl(); echo $tpl->__invoke('page.tpl.php', ['title' => 'Foobar']);
page.tpl.php
:
<?php /** @var Xtompie\Tpl\Tpl $this */ ?> <h1><?= $this->e($title) ?></h1>
- 使用原生PHP
- 不自动转义,但可以使用
vendor/bin/xtompie-tpl-audit.sh -p src -s .tpl.php
- 无章节
- 无块
- 无命名空间
- 使用
<?php /** @var Xtompie\Tpl\Tpl $this */ ?>
进行自动补全
要求
PHP >= 8.0
安装
使用composer
composer require xtompie/tpl
完整功能示例
// Extend Tpl for customization, set templatePathPrefix, add helpers // src/App/Shared/Tpl/Tpl.php namespace App\Shared\Tpl\Tpl; use Xtompie\Tpl\Tpl as BaseTpl; class Tpl extends BaseTpl { protected function templatePathPrefix(): string { return 'src/'; } protected function date(int $time): string { return $this->e(date('Y-m-d H:i:s', $time)); } } // src/App/Test/Ui/Controller/TestController.php namespace App\Test\Ui\Controller; use App\Shared\Tpl\Tpl; class TestController { public function __construct( private Tpl $tpl ) { } public function __invoke(): string { return $this->tpl->__invoke('Test/UI/Tpl/content.tpl.php', ['title' => 'foobar']); } } // src/Test/UI/Tpl/content.tpl.php - first level // keep template file names with `.tpl.php` suffix e.g. easy exclude from phpstan <?php /** @var App\Shared\Tpl\Tpl $this */ ?> <?php $this->push('Test/UI/Tpl/layout.tpl.php', ['title' => $title]); ?> <h1><?= $this->e($title) ?></h1> // src/Test/UI/Tpl/layout.tpl.php - second level <?php /** @var App\Shared\Tpl\Tpl $this */ ?> <?php $this->push('Test/UI/Tpl/head.tpl.php', ['title' => $title]); ?> <div class="container"> <?= $this->render('Test/UI/Tpl/navbar.tpl.php') ?> <?= $this->content() ?> </div> // src/Test/UI/Tpl/navbar.tpl.php <nav> <a href="/">Index</a> </nav> // src/Test/UI/Tpl/head.tpl.php - third level <?php /** @var App\Shared\Tpl\Tpl $this */ ?> <html> <head> <title><?= $this->e($title) ?></title> </head> <body> <?= $this->content() ?> </body> </html>