https://github.com/inteve/simple-components
Simple independent components for Latte/Nette.
https://github.com/inteve/simple-components
latte nette nette-latte php
Last synced: 5 months ago
JSON representation
Simple independent components for Latte/Nette.
- Host: GitHub
- URL: https://github.com/inteve/simple-components
- Owner: inteve
- License: other
- Created: 2021-03-30T15:46:57.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-07-01T10:02:11.000Z (12 months ago)
- Last Synced: 2025-07-01T10:25:02.622Z (12 months ago)
- Topics: latte, nette, nette-latte, php
- Language: PHP
- Homepage:
- Size: 36.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license.md
Awesome Lists containing this project
README
# Inteve\SimpleComponents
[](https://github.com/inteve/simple-components/actions)
[](https://packagist.org/packages/inteve/simple-components)
[](https://github.com/inteve/simple-components/releases)
[](https://github.com/inteve/simple-components/blob/master/license.md)
Simple independent components for Latte templates.
## Installation
[Download a latest package](https://github.com/inteve/simple-components/releases) or use [Composer](http://getcomposer.org/):
```
composer require inteve/simple-components
```
Inteve\SimpleComponents requires PHP 8.0 or later.
## Usage
### 1. create components factory
```php
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:**
```php
$latte = new Latte\Engine;
$componentFactory = new MyComponentFactory;
\Inteve\SimpleComponents\LatteMacros::installToLatte($latte, $componentFactory);
```
**In Nette presenter:**
```php
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
```latte
{block content}
My Page
{component menu}
{component breadcrumbs, items => $breadcrumbItems}
Lorem ipsum dolor sit amet.
{/block}
```
## Prepared implementations
### `DirectoryFactory`
Loads template files from specified directory.
```
/app
/components
breadcrumbs.latte
menu.latte
```
```php
$componentFactory = new SimpleComponents\DirectoryFactory('/path/to/app/components');
```
```latte
{component menu}
{component breadcrumbs}
```
### `MultiFactory`
Packs multiple `ComponentFactory` implementations to one class.
```php
$componentFactory = new SimpleComponents\MultiFactory([
new MyComponentFactory,
new SimpleComponents\DirectoryFactory('/path/to/app/components')
]);
```
```latte
{component menu}
{component breadcrumbs}
{component someMyComponent}
```
## Typed templates
```php
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;
}
}
```
```latte
{component breadcrumbs, items => $breadcrumbsItems}
```
------------------------------
License: [New BSD License](license.md)
Author: Jan Pecha, https://www.janpecha.cz/