pine3ree / pine3ree-plates-resolvers
Plates 引擎的自定义模板解析器
Requires
- php: ^7.4 || ^8.0 || ^8.1 || ^8.2
- league/plates: ^3.5
Requires (Dev)
- mikey179/vfsstream: ^1.6.11
- phpspec/prophecy-phpunit: ^1.1 || ^2.0
- phpstan/phpstan: ^1.10
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^7.5 || ^8.0 || ^9.0
- squizlabs/php_codesniffer: ^3.5.7
- vimeo/psalm: ^5.13
- webimpress/coding-standard: ^1.3
This package is auto-updated.
Last update: 2024-09-14 03:23:32 UTC
README
此包提供了一些带有缓存支持的模板解析器,用于 Plates 引擎
-
NameAndFolderResolveTemplatePath
与默认的 Plates 解析器相同,但启用了缓存 -
ReverseFallbackResolveTemplatePath
是一个逆向工作的解析器:首先搜索全局模板目录(如果已定义),然后搜索模板文件夹
安装
此包需要 PHP 7.4 ~ 8.2
。您可以通过 composer 安装它
$ composer require pine3ree/pine3ree-plates-resolvers
NameAndFolderResolveTemplatePath
与默认的 Plates 模板路径解析器工作方式相同,但增加了已解析模板的缓存支持。
内部缓存存储并按名称返回那些已成功解析的模板路径,从而避免了在相同的模板上重复调用 $name->getPath()
。内部缓存在此包中通过启用 CacheableResolveTemplatePathTrait trait
以及实现 CacheableResolveTemplatePathInterface
的方法来启用
这在您在同一个页面上多次使用部分(如排序表格标题链接或多个分页器)时很有用。同时,它也为异步环境(如 Swoole
)提供了长期缓存
ReverseFallbackResolveTemplatePath
此解析器的工作方式与默认的 Plates 解析器相反
当使用文件夹规范(Plates ::
表示法)渲染模板时,搜索从默认模板目录(如果已定义)开始,但添加了一个与文件夹规范匹配的子文件夹,例如
$template->render('partials::pagination', $vars); // The search order is: // // 1. {/path/to/the/default/templates/directory/}partials/pagination.phtml // 2. {/path/to/the/partials/template/folder/}pagination.phtml
此外,当提供一个不带文件夹规范但包含路径分隔符 "/" 的模板名称时,将使用第一个段来分配文件夹。因此,前面的示例也适用于以下更简单的渲染调用
$template->render('partials/pagination', $vars);
在两种情况下,“部分”文件夹必须在引擎配置中定义。
这在模块化应用程序中可能很有用,其中我们希望每个模块的模板靠近源代码。在这种情况下,我们为每个模块模板设置一个文件夹。当我们将在其他应用程序中重复使用相同的模块时,它将立即工作。然后,我们将使用默认的“应用程序”模板目录来定制/覆盖每个模块提供的默认模板。
示例
给定以下应用程序目录结构
/path/to/my/web/app/
templates/ <-- global templates directory
news/
article/
read.phtml <-- This template will be LOADED
...
shop/
product/
<-- no index.phtml here -->
...
News/ <-- module dir
Controller/
Article/
ReadController.php
Model/
etc/
templates/ <-- local module templates folder
article/
read.phtml <-- This template will be DISCARDED
...
Shop/ <-- module dir
Controller/
Product/
IndexController.php
Model/
etc/
templates/ <-- local module templates folder
product/
index.phtml <-- This template will be LOADED
...
以下代码适用
$plates = new League\Plates\Engine('/path/to/my/web/app/templates'); $plates->addFolder('news', '/path/to/my/web/app/News/templates'); $plates->addFolder('shop', '/path/to/my/web/app/Shop/templates'); // This will load the template in the global templates directory, because a // matching template-file is found $plates->render("news::article/read", $vars); // or simply $plates->render("news/article/read", $vars); // This will load the template in the module templates directory, because there // is no matching file in the global directory $plates->render("shop::product/index", $vars); // or simply $plates->render("shop/product/index", $vars);
推荐使用 URL 路径 "/" 段表示法,因为它显示了相对于全局模板目录的模板路径,而标准的文件夹表示法可能会让人误以为没有使用全局目录模板。