pkit / phantom
一个用于处理视图的简单模块
v0.1.1
2024-02-15 21:41 UTC
Requires
- php: >=8.1
Requires (Dev)
- pestphp/pest: ^2.12
This package is auto-updated.
Last update: 2024-09-15 22:57:29 UTC
README
一个用于处理视图的简单模块
安装
使用Composer,输入以下命令
composer require pkit/phantom
用法
渲染
最初,页面通过静态类 Pkit\Phantom
渲染。
use Pkit\Phantom; use Pkit\View; # ".\view" is the default directory, and views are discovered without file extension return Phantom::render("page", ["var" => $value]); /***/ return Phantom::renderView(new View(/* path */), "dir/page");
页面
页面是用纯PHP编写的,使用的扩展是 .phtml
,无法修改。
<? use Pkit\Phantom ?> <? /* Passed variables are already converted */ ?> <?= $var ?> <? /* Views from other folders are supported */ ?> <?= Phantom::renderView(new View(/* path */), "dir/page") ?>
组件
# ./view/component.phtml component <?= $var ?>
要添加组件,基本上使用 include
方法。
# ./view/master.phtml <? use Pkit\Phantom ?> <?= Phantom::include("component", ["var" => 1]) ?>
部分
可以使用插槽语法创建模板。
# ./view/master.phtml <? use Pkit\Phantom ?> <? //* Slots must have unique keys to avoid conflicts */ ?> <tag> <?= Phantom::slot("slot_1") ?> </tag> <?= Phantom::slot("slot_2") ?>
要在页面上使用它们,应该扩展视图,因此通过包裹部分,它们将被传递到插槽中。
<? use Pkit\Phantom ?> <? Phantom::extend("master")?> <? Phantom::section("slot_1") ?> # text... <? Phantom::stop() ?> # The text in these intervals will be ignored # Note: the code in this area will still be executed <? Phantom::section("slot_2") ?> # text... <? Phantom::stop() ?>