Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/starychfojtu/viewcomponentbundle
Implementation of View Components for Symfony 3.3
https://github.com/starychfojtu/viewcomponentbundle
component components symfony symfony-bundle symfony3 symfony3-bundle twig twig-extension viewcomponent
Last synced: about 1 month ago
JSON representation
Implementation of View Components for Symfony 3.3
- Host: GitHub
- URL: https://github.com/starychfojtu/viewcomponentbundle
- Owner: starychfojtu
- License: mit
- Created: 2017-06-18T12:14:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-09-25T07:51:52.000Z (about 7 years ago)
- Last Synced: 2024-10-13T06:21:50.562Z (about 1 month ago)
- Topics: component, components, symfony, symfony-bundle, symfony3, symfony3-bundle, twig, twig-extension, viewcomponent
- Language: PHP
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ViewComponentBundle
Implementation of View Components for Symfony 3.3# Installation
```
$ composer require starychfojtu/viewcomponent
```Symfony flex might register incorrect namespace in bundles.php. If it did,
go ahead and register it yourself with
```php
'ViewComponent\ViewComponentBundle' => ['all' => true]
```# Configuration
```yaml
starychfojtu_view_component:
component_dirs: ['AppBundle/Component', 'AppBundle/SpecialComponent'] #results in '/src/AppBundle/Component', '/src/AppBundle/Component/specialComponent'
# Specify directories where the bundle should search for components from /src
template_dirs: ['components', 'specialComponents'] #results in '/templates/components', '/templates/specialComponents'
# Specify directories where the bundle should search for templates from /templates
```# Usage
First specify your view component by creating a class in configured directories
and implement ```ViewComponentInterface```. The render method returns associative array of
objects that are passed to the view.YOU HAVE TO NAME YOUR COMPONENT IN THIS WAY : YourSpecialNameViewComponent
It is a classic CamelCase naming e.g. MenuViewComponent or CartSummaryViewComponent.```php
# src/AppBundle/Component/MenuViewComponent['home','about','contact']
];
}
}```
Then add your template to one of configured directories.
```twig
# templates/components/Menu.html.twig{% for link in links %}
{{ link }}
{% endfor %}
```And finally render your component in the view:
```twig
{{ component('Menu', 60 * 60) }}
```The second parameter is cache time of the whole component with default set on 0;
## Custom template name
If you want to specify another template name in view component, just add
a key with template like this:```php
# src/AppBundle/Component/MenuViewComponent['home','about','contact'],
'template' => 'AnotherMenu'
];
}
}```
## Dependency injection
To pass any dependencies to the component, just register them as services.
TODO: register them automatically in bundle