inteve / simple-components
Latte 模板简单组件
v1.0.0
2023-05-30 12:53 UTC
Requires
- php: >=5.6.0
- latte/latte: ^2.4
Requires (Dev)
- nette/tester: ^2.0
This package is auto-updated.
Last update: 2024-09-20 14:22:14 UTC
README
Latte 模板的简单独立组件。
安装
composer require inteve/simple-components
Inteve\SimpleComponents 需要 PHP 5.6.0 或更高版本。
使用方法
1. 创建组件工厂
use Inteve\SimpleComponents; class MyComponentFactory implements SimpleComponents\ComponentFactory { public function create($componentName, array $args = []) { if ($componentName === 'menu') { return new SimpleComponents\GenericComponent(__DIR__ . '/components/Menu.latte'); } elseif ($componentName === 'breadcrumbs') { return new SimpleComponents\GenericComponent(__DIR__ . '/components/Breadcrumbs.latte', $args); } return NULL; } }
2. 注册 {component}
宏
纯 PHP
$latte = new Latte\Engine; $componentFactory = new MyComponentFactory; \Inteve\SimpleComponents\LatteMacros::installToLatte($latte, $componentFactory);
Nette 展示器
abstract class BasePresenter extends \Nette\Application\UI\Presenter { /** @var \Inteve\SimpleComponents\ComponentFactory @inject */ public $componentFactory; protected function createTemplate() { $template = parent::createTemplate(); assert($template instanceof \Nette\Bridges\ApplicationLatte\Template); \Inteve\SimpleComponents\LatteMacros::installToLatte($template->getLatte(), $this->componentFactory); return $template; } }
3. 在您的应用模板中使用它
{block content} <h1>My Page</h1> {component menu} {component breadcrumbs, items => $breadcrumbItems} <p>Lorem ipsum dolor sit amet.</p> {/block}
预准备实现
DirectoryFactory
从指定目录加载模板文件。
/app
/components
breadcrumbs.latte
menu.latte
$componentFactory = new SimpleComponents\DirectoryFactory('/path/to/app/components');
{component menu} {component breadcrumbs}
MultiFactory
将多个 ComponentFactory
实现打包到一个类中。
$componentFactory = new SimpleComponents\MultiFactory([ new MyComponentFactory, new SimpleComponents\DirectoryFactory('/path/to/app/components') ]);
{component menu} {component breadcrumbs} {component someMyComponent}
类型化模板
class Breadcrumbs implements SimpleComponents\Component { /** @var BreadcrumbItem[] */ private $items; /** * @param BreadcrumbItem[] $items */ public function __construct(array $items) { $this->items = $items; } public function getFile() { return __DIR__ . '/components/breadcrumbs.latte'; } public function getParameters() { return [ 'items' => $this->items; ]; } } class MyComponentFactory implements SimpleComponents\ComponentFactory { public function create($componentName, array $args = []) { if ($componentName === 'breadcrumbs') { return new Breadcrumbs($args['items']); } return NULL; } }
{component breadcrumbs, items => $breadcrumbsItems}
许可协议: 新BSD许可协议
作者:Jan Pecha,https://www.janpecha.cz/