Single Directory Components for Symfony UX: a component's PHP class, Twig template, CSS and JavaScript live in one directory, and the CSS and JS reach the page only when the component is rendered on it.
src_component/
└── Alert/
├── Alert.php # the component class
├── Alert.html.twig # its template, found without configuration
├── Alert.css # added to <head> when an Alert is rendered
└── Alert_controller.js # its Stimulus controller (optional)
The bundle connects Twig Components
with AssetMapper: it
registers the component directory with Twig, AssetMapper and StimulusBundle, finds
the files next to each component class (or template, for an anonymous component),
and injects the <link> and <script> tags of the components a response actually
rendered — into the page, or for Live Components and Turbo fragments, through a
response header the bundle's Stimulus controller reads.
The idea comes from Hugo Alliaume's article A Better Architecture for Your Symfony UX Twig Components. It is used in production on mostka.sk, formalitka.mostka.sk and ycon.cc.
PHP 8.1+, Symfony 6.4, 7.x or 8.x, symfony/ux-twig-component 2.20+ or 3.x, and
AssetMapper (Webpack Encore is not supported). StimulusBundle, LiveComponent,
MakerBundle and UX Icons are optional. See Installation.
composer require tito10047/ux-sdcA Flex recipe is proposed to
symfony/recipes-contrib. It configures the bundle, imports its routes and adds
{{ render_component_assets() }} to templates/base.html.twig; what is left to
you — the Composer PSR-4 entry, and for Live Components or Turbo the loader
controller on <body> — it prints. Until it is available, or without Flex,
follow Without the recipe: five short
steps.
With App\Component\ mapped to src_component/ and the bundle configured as
above, src_component/Alert/Alert.php:
<?php
namespace App\Component\Alert;
use Tito10047\UX\Sdc\Attribute\AsSdcComponent;
#[AsSdcComponent]
final class Alert
{
public string $type = 'info';
public string $message;
}src_component/Alert/Alert.html.twig:
<div {{ attributes.defaults({class: 'alert alert-' ~ type}) }}>
{{ message }}
</div>src_component/Alert/Alert.css:
.alert { padding: 1rem; border-radius: .25rem; }
.alert-success { background: #e6f4ea; }Render it from any template:
<twig:Alert:Alert type="success" message="Saved." />A page that renders an Alert gets
<link rel="stylesheet" href="/assets/Alert/Alert-….css" data-turbo-track="dynamic">
in its <head> and a Link: </assets/Alert/Alert-….css>; rel="preload"; as="style"
header. A page without an Alert gets neither.
php bin/console make:sdc-component Alert generates such a skeleton, and
php bin/console debug:sdc shows what each component gets.
- Installation — requirements, the Flex recipe, manual setup
- Components — names, co-located files, templates, Stimulus controllers, where components can live, anonymous components
- Assets —
#[AsSdcComponent],#[Asset], ES modules, Sass, font and image preloads, CSP nonces - Component icons — SVG icons in the component's folder with
symfony/ux-icons,
ux:sdc:icons:import(experimental) - Live Components and Turbo — the loader controller and
the
X-SDC-Assetsheader - Generating components —
make:sdc-component, page components with--action - Configuration — every option
- Diagnostics —
debug:sdc, logs, the web profiler panel - How it works — the build-time and runtime pipelines, performance
- Known limitations and non-goals
- Upgrading from 0.4 to 1.0 and the changelog
tito10047/ux-sdc-skills teaches
coding agents how components are discovered, how their assets reach the browser
and how to diagnose the failure modes. In Claude Code:
/plugin marketplace add tito10047/ux-sdc-skills, then
/plugin install ux-sdc-skills. For other agents it is a plain directory of
Markdown in the agentskills.io layout, with AGENTS.md,
CLAUDE.md and llms.txt.
The bundle follows Semantic Versioning. From 1.0, a minor or patch release does not break:
- Configuration: the
ux_sdcoptions, their defaults and accepted values (Configuration), and theux_sdc.ux_components_dirparameter. - PHP:
Attribute\AsSdcComponentandAttribute\Asset(with itsCSS,JS,FONTandIMAGEconstants), theTwig\Stimulustrait,Twig\ComponentNamespaceInterface,Csp\NonceProviderInterface,Csp\NelmioNonceProvideras a value ofcsp_nonce_provider,Service\AssetRegistry::isolate()(the service of that class), andSdcBundlewithSdcBundle::STIMULUS_CONTROLLER— all inTito10047\UX\Sdc. - Twig: the
render_component_assets()function, thesdc_loader_controllerglobal, thecontrollervariable and how its value is derived, how components and their co-located files are named. - HTTP: the
X-SDC-Assetsheader, format version 1, and the loader Stimulus controller that reads it. - Routing: the
type: ux_sdcroute import. - Logging: the
ux_sdclog channel. - Console: the names, arguments and options of
debug:sdcandmake:sdc-component, the exit codes and the JSON structure ofdebug:sdc.
Not covered, and free to change in any release: the experimental
component icons (the icons options, the sdc: icon names and
ux:sdc:icons:import), every class, method and constant
marked @internal (all of src/ not listed above), service IDs, other container
parameters, the format of the metadata cache, the text output of debug:sdc, the
wording of log messages and the code make:sdc-component generates. A minor
release may add debug:sdc checks, so --strict may find problems it did not
find before. Something covered is deprecated in a minor release, with a deprecation
notice where possible, and removed in the next major one.
Bug reports and pull requests are welcome on
GitHub. Run ./vendor/bin/phpunit,
./vendor/bin/phpstan analyse and ./vendor/bin/php-cs-fixer fix --dry-run
before opening a pull request.
MIT — see LICENSE.md.