Installation
composer require cooolinho/symfony-ui-bundle
Register the bundle in config/bundles.php (Symfony):
return [
// ...
Coolinho\SymfonyUiBundle\CoolinhoSymfonyUiBundle::class => ['all' => true],
];
First Use Case
SymfonyUiManager service into a controller or Twig template:
use Coolinho\SymfonyUiBundle\Manager\SymfonyUiManager;
class MyController extends AbstractController
{
public function index(SymfonyUiManager $uiManager): Response
{
$ui = $uiManager->render('button', ['label' => 'Click Me']);
return $this->render('template.html.twig', ['ui' => $ui]);
}
}
In Twig:
{{ ui|raw }}
Where to Look First
src/Resources/doc/ for usage examples.src/Component/ for built-in components like Button, Alert, or Modal.config/packages/cooolinho_symfony_ui.yaml for defaults.Dynamic UI Rendering
SymfonyUiManager to generate reusable components dynamically:
$uiManager->render('card', [
'title' => 'User Profile',
'content' => $user->bio,
'actions' => [
['type' => 'button', 'label' => 'Edit', 'href' => '/edit']
]
]);
Twig Integration
{# Render a button with Twig logic #}
{% set button = ui('button', {label: 'Save', icon: 'save'}) %}
{{ button|render }}
// src/Twig/UiExtension.php
class UiExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions(): array
{
return [
new \Twig\TwigFunction('ui', [$uiManager, 'render']),
];
}
}
Component Composition
$uiManager->render('modal', [
'title' => 'Confirm Action',
'body' => $uiManager->render('alert', ['message' => 'Are you sure?']),
'footer' => $uiManager->render('button', ['label' => 'Confirm', 'type' => 'primary'])
]);
Event-Driven UI
$uiManager->render('button', [
'label' => 'Toggle',
'onClick' => 'toggleModal()'
]);
data-* attributes for custom logic:
$uiManager->render('div', [
'class' => 'custom-element',
'data-action' => 'fetch-data'
]);
$form = $formFactory->createNamedBuilder('user', UserType::class, $user)
->getForm();
$ui = $uiManager->render('form', ['form' => $form->createView()]);
$uiManager->render('script', [
'src' => $assetManager->getUrl('bundles/cooolinho/js/ui.js')
]);
$uiManager->render('button', [
'label' => 'app.ui.save',
'translator' => $translator
]);
Component Registration
services.yaml:
services:
Coolinho\SymfonyUiBundle\Component\MyCustomComponent:
tags: ['cooolinho.ui.component']
Twig Auto-escaping
& becomes &).|raw in Twig or configure Twig’s escaping strategy:
{{ ui|raw }}
Or in PHP:
$uiManager->render('div', ['safe' => true]);
Dependency Injection
SymfonyUiManager not injectable.config/bundles.php and clear cache:
php bin/console cache:clear
JavaScript Conflicts
$uiManager->render('div', [
'class' => 'ui-container',
'data-isolation' => true
]);
COOLINHO_UI_DEBUG=true in .env to log component rendering.file_put_contents(
'debug-ui.html',
$uiManager->render('button', ['label' => 'Test'])
);
src/Component/ or are registered via tags.Default Component Paths
src/Component/ or paths defined in cooolinho_symfony_ui.yaml:
coolinho_symfony_ui:
component_paths: ['%kernel.project_dir%/src/Components']
Overriding Defaults
class CustomButton extends \Coolinho\SymfonyUiBundle\Component\Button
{
public function render(array $options): string
{
$options['class'] = 'btn-custom ' . ($options['class'] ?? '');
return parent::render($options);
}
}
services.yaml with a higher priority.Performance
$cache = $cacheManager->getItem('ui_button_cache');
if (!$cache->isHit()) {
$cache->set($uiManager->render('button', [...]));
$cache->expiresAfter(3600);
}
Custom Components
AbstractComponent:
namespace App\Component;
use Coolinho\SymfonyUiBundle\Component\AbstractComponent;
class ProgressBar extends AbstractComponent
{
public function render(array $options): string
{
return sprintf(
'<progress value="%s" max="100">%s%%</progress>',
$options['value'] ?? 0,
$options['value'] ?? 0
);
}
}
services:
App\Component\ProgressBar:
tags: ['cooolinho.ui.component']
Component Modifiers
UiRenderEvent:
$eventDispatcher->addListener(
'cooolinho.ui.render',
function (UiRenderEvent $event) {
$event->setOutput(str_replace('class="btn"', 'class="btn modified"', $event->getOutput()));
}
);
Asset Bundling
# config/packages/cooolinho_symfony_ui.yaml
coolinho_symfony_ui:
assets:
js: ['/bundles/cooolinho/js/custom.js']
css: ['/bundles/cooolinho/css/custom.css']
How can I help you explore Laravel packages today?