inteve/simple-components

Simple components for Latte templates

Fund package maintenance!
Other

v1.0.0 2023-05-30 12:53 UTC

This package is auto-updated.

Last update: 2024-12-20 14:48:24 UTC


README

Build Status Downloads this Month Latest Stable Version License

Simple independent components for Latte templates.

Donate

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/