inteve/simple-components
最新稳定版本:v1.0.0
Composer 安装命令:
composer require inteve/simple-components
包简介
Simple components for Latte templates
README 文档
README
Simple independent components for Latte templates.
Installation
Download a latest package or use Composer:
composer require inteve/simple-components
Inteve\SimpleComponents requires PHP 8.0 or later.
Usage
1. create components factory
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. register {component} macro
In plain PHP:
$latte = new Latte\Engine; $componentFactory = new MyComponentFactory; \Inteve\SimpleComponents\LatteMacros::installToLatte($latte, $componentFactory);
In Nette presenter:
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. use it in your app template
{block content} <h1>My Page</h1> {component menu} {component breadcrumbs, items => $breadcrumbItems} <p>Lorem ipsum dolor sit amet.</p> {/block}
Prepared implementations
DirectoryFactory
Loads template files from specified directory.
/app
/components
breadcrumbs.latte
menu.latte
$componentFactory = new SimpleComponents\DirectoryFactory('/path/to/app/components');
{component menu} {component breadcrumbs}
MultiFactory
Packs multiple ComponentFactory implementations to one class.
$componentFactory = new SimpleComponents\MultiFactory([ new MyComponentFactory, new SimpleComponents\DirectoryFactory('/path/to/app/components') ]);
{component menu} {component breadcrumbs} {component someMyComponent}
Typed templates
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}
License: New BSD License
Author: Jan Pecha, https://www.janpecha.cz/
统计信息
- 总下载量: 2.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2021-03-30