inteve / simple-components
Simple components for Latte templates
Fund package maintenance!
Other
Installs: 1 893
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: >=5.6.0
- latte/latte: ^2.4
Requires (Dev)
- nette/tester: ^2.0
This package is auto-updated.
Last update: 2024-12-20 14:48:24 UTC
README
Simple independent components for Latte templates.
Installation
Download a latest package or use Composer:
composer require inteve/simple-components
Inteve\SimpleComponents requires PHP 5.6.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/