Simple independent components for Latte templates.
Download a latest package or use Composer:
composer require inteve/simple-components
Inteve\SimpleComponents requires PHP 5.6.0 or later.
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;
}
}
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;
}
}
{block content}
<h1>My Page</h1>
{component menu}
{component breadcrumbs, items => $breadcrumbItems}
<p>Lorem ipsum dolor sit amet.</p>
{/block}
Loads template files from specified directory.
/app
/components
breadcrumbs.latte
menu.latte
$componentFactory = new SimpleComponents\DirectoryFactory('/path/to/app/components');
{component menu}
{component breadcrumbs}
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}
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/