-
-
Notifications
You must be signed in to change notification settings - Fork 315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TwigComponent] Support for SubDirectory-named components via namespace config #1140
Conversation
1/ We don't find many plurals in symfony "default" structure.. not a preference but 2/ 100% for UpperCamel as default case So maybe
|
It is a bit odd - we should just decide one way or another. MakerBundle currently generates into |
The symfony demo uses "componentS" too... but it may be a unique thing in the symfony codebase. (again, i have no personal opinion on the matter) |
I prefer Component (without the "S"), so it's consistent with src/Entity, src/EventSubscriber, src/Form. I've been working on a bundle the incorporates some common bootstrap components (Alert, Accordion, Badge, etc.), and I'm wondering if there's a way to autoregister subdirectories and even anonymous components. For example, right now I have foreach (
[
AlertComponent::class,
AccordionComponent::class,
AlertComponent::class,
BadgeComponent::class,
ButtonComponent::class,
CardComponent::class,
CarouselComponent::class,
DropdownComponent::class,
DividerComponent::class,
LinkComponent::class
] as $componentClass
) {
$builder->register($componentClass)->setAutowired(true)->setAutoconfigured(true);
} But I'm starting to subdivide the component for a bootstrap themes (tabler, snead, adminkit, etc.), to replace the embed/include/macro setup that's common in most layout.html.twig files. But now I have event more components:
So of course I love the idea of this PR, so I can have Tabler\Navbar.php, Tabler\PageHeader.php, etc. Most macros port very easily to anonymous components, but also most macros I use are in bundles, so it'd be cool if there were a way when initializing the bundle there were a way to scan the templates directory and register the templates as anonymous components. |
Replaced by #1151 |
Recipes PR: symfony/recipes#1243
Currently, you can render an anonymous component with a
:
syntax, which becomes a subdirectory:But if you want to add a class for this component later, you now must use the
name: 'Button:Primary'
key. That's because, even if the component lives inApp\Twig\Components\Button\Primary
, the automatic component name will just bePrimary
.New Features & Conventions from this PR
A) New Config
You configure the "namespace root" and templates directory for your components. Default will be:
If you don't have this config, you will get the same "class name === component name" behavior of today, but with a deprecation notice.
B) SubDirectory Naming
Thanks to the above, component names now take into account "subdirectories":
App\Twig\Components\Alert
Alert
App\Twig\Components\Button\Primary
Button:Primary
Best-Practices / Conventions
App\Twig\Components\
as the default namespace/directory for components.What do people think? What are you doing in your apps today?
You can do whatever you want, but I'm proposing that we continue to use the convention where component name, class and template all use UpperCamelCase naming:
I've also updated and reorganized the TwigComponent docs quite a lot - we had some great sections that, over time, were not in the ideal order.
Also, the TwigComponent docs now use the
<twig:HTML>
syntax almost exclusively.Cheers!
TODO