jascha030 / wp-sequoia
静态HTML组件库,使用OOP Php结合Twig模板语言
Requires
- php: ^7.4
- symfony/finder: ^5.3
- twig/twig: ^3.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-10 17:25:54 UTC
README
这是一个针对PHP的轻量级组件库,使用Twig模板语言库和面向对象方法。专为Wordpress设计,但不仅限于Wordpress。
先决条件
- Php
^7.4 || ^8.0 - Composer
^2.0
安装
通过以下方式作为依赖项要求;直接添加到您的composer.json中,或运行
composer require jascha030/wp-sequoia
就这么简单…
使用方法
所有内容都基于两个接口
TwigTemplaterInterface 和 TwigComponentInterface 及其主要实现 TwigComponentAbstract。
模板器
创建一个需要Twig\Environment实例的模板器。(建议使用FilesystemLoader)。
示例
<?php use Jascha030\Sequoia\Templater\TwigTemplater; use Twig\Environment; use Twig\Loader\FilesystemLoader; // Replace with the path to your templates folder. $loader = new FilesystemLoader('/Twig/Template/Folder'); $environment = new Environment($loader); $templater = new TwigTemplater($environment);
假设你在模板文件夹中有一个名为list-template.twig的模板
<ul> {% for item in items %} <li>{{ item.text }}</li> {% endfor %} </ul>
现在你可以扩展TwigComponentAbstract。
<?php namespace Example\Component; use Jascha030\Sequoia\Component\TwigComponentAbstract; final class ListComponent extends TwigComponentAbstract { /** * @inheritDoc */ public function getTemplate(): string { return 'list-template.twig'; } }
它可以通过以下方式渲染
<?php use Example\Component\ListComponent; use Jascha030\Sequoia\Templater\TwigTemplater; use Twig\Environment; use Twig\Loader\FilesystemLoader; // Replace with the path to your templates folder. $loader = new FilesystemLoader('/Twig/Template/Folder'); $environment = new Environment($loader); $templater = new TwigTemplater($environment); ListComponent::render( $templater, [ 'items' => [ ['text' => 'List item 1'], ['text' => 'List item 2'] ] ] );
使用默认值
你也可以为组件提供默认值,使用HasDefaultContextTrait,例如
… final class ListComponent extends TwigComponentAbstract { use HasDefaultContextTrait; /** * @inheritDoc */ public function getTemplate(): string { return 'list-template.twig'; } /** * @inheritDoc */ final public function getDefaults(): array { return [ 'items' => [ ['text' => 'List item 1'], ['text' => 'List item 2'] ] ]; } }
与Wordpress结合使用
当可用时,渲染中使用的getContext方法将有一个自动生成的过滤器,用于根据以下模式修改默认值
twig_template_context_{$this->getTemplateSlug()}
其中$this->getTemplateSlug()将默认返回在getTemplate()方法中定义的模板名称,去除.twig后缀,在上面的示例中,这将是:twig_template_context_list-template,然后可以在以下上下文中使用(在Wordpress中)
function edit_list_items(array $context): array { $context['items'][] = ['text' => 'List item 3']; return $context; } \add_filter('twig_template_context_list-template', 'edit_list_items');
现在假设你添加了一个第三列表项,ListComponent::render()方法的输出将是
<ul> <li>List item 1</li> <li>List item 2</li> <li>List item 3</li> </ul>
运行单元测试
为了检查你的实现是否正确,或者在使用过程中是否有任何错误,你可以使用
composer run phpunit
请确保使用
composer install而不是composer install —no-dev安装开发依赖项。
更多内容即将推出。
灵感
这是基于对Wordpress、他们混乱的模板系统、混乱的用户群体以及近年来很少更新的timber/timber库及其过度复杂的特性集感到沮丧而开始的库。
它非常具体,主要为我本人以及让前端功能开发更有趣而构建。
但也许有人会在这个库中找到一些用途,我希望扩展它并添加大量特性,直到它变得和timber一样过度复杂。
话虽如此,timber/timber是一个很棒的库,所以如果你使用它,请支持它。尽管Wordpress混乱且过时的本质,但它对于那些学习或不想成为开发者的人来说,是一个构建网站的伟大工具,这也是它取得巨大成功的原因。
对于那些想成为开发者的人来说,请注意Wordpress的编码风格是很好的起点,它们并不是我们在世界其他地方开发代码的方式,并且基于早已被遗忘的标准。
向那些在互联网上找到并教授如何以更现代的方式开发Wordpress的几位英雄致敬。
如果你是那些渴望成为开发者或对使用Wordpress感到无聊的人之一,我建议你谷歌一下以下人:
- Carl Alexander
- Josh Pollock
- Roots团队(来自bedrock,sage)