Skip to content

Commit bc72ec7

Browse files
committed
feature #460 [Twig] add a component name/template : "namespace" notation (kbond)
This PR was merged into the 2.x branch. Discussion ---------- [Twig] add a component name/template `:` "namespace" notation | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | #108 | License | MIT Proposes `:` as a namespace separator for component names. The default template replaces the `:` with `/` to create sub-directories. A component with the name `form:input`'s default template would be `templates/components/form/input.html.twig`. Don't believe this would be a BC break. If someone is using the `:` notation already, they likely would have customized the template. Commits ------- b9e1554 [Twig] add a component name/template "namespace" notation
2 parents 5f484d6 + b9e1554 commit bc72ec7

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

src/TwigComponent/src/DependencyInjection/Compiler/TwigComponentPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function process(ContainerBuilder $container): void
4141

4242
$tag['service_id'] = $id;
4343
$tag['class'] = $definition->getClass();
44-
$tag['template'] = $tag['template'] ?? "components/{$tag['key']}.html.twig";
44+
$tag['template'] = $tag['template'] ?? sprintf('components/%s.html.twig', str_replace(':', '/', $tag['key']));
4545
$componentConfig[$tag['key']] = $tag;
4646
}
4747
}

src/TwigComponent/src/Resources/doc/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ as the second argument to the ``AsTwigComponent`` attribute:
190190
// ...
191191
}
192192
193+
Twig Template Namespaces
194+
~~~~~~~~~~~~~~~~~~~~~~~~
195+
196+
You can use a ``:`` in your component's name to indicate a namespace. The default
197+
template will replace the ``:`` with ``/``. For example, a component with the name
198+
``form:input`` will look for a template in ``templates/components/form/input.html.twig``.
199+
193200
The mount() Method
194201
~~~~~~~~~~~~~~~~~~
195202

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\TwigComponent\Tests\Fixtures\Component;
13+
14+
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
15+
16+
#[AsTwigComponent('foo:bar:baz')]
17+
final class NamespacedComponent
18+
{
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Content...

src/TwigComponent/tests/Integration/ComponentExtensionTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public function testCanRenderEmbeddedComponent(): void
130130
$this->assertStringContainsString('custom td (1)', $output);
131131
}
132132

133+
public function testComponentWithNamespace(): void
134+
{
135+
$output = $this->renderComponent('foo:bar:baz');
136+
137+
$this->assertStringContainsString('Content...', $output);
138+
}
139+
133140
private function renderComponent(string $name, array $data = []): string
134141
{
135142
return self::getContainer()->get(Environment::class)->render('render_component.html.twig', [

0 commit comments

Comments
 (0)