Skip to content

Commit 8d43965

Browse files
committed
[Twig] add a component name/template "namespace" notation
1 parent 5f484d6 commit 8d43965

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

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

Lines changed: 6 additions & 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'] ?? self::normalizeDefaultTemplatePath($tag['key']);
4545
$componentConfig[$tag['key']] = $tag;
4646
}
4747
}
@@ -50,4 +50,9 @@ public function process(ContainerBuilder $container): void
5050
->setArgument(2, $componentConfig)
5151
;
5252
}
53+
54+
private static function normalizeDefaultTemplatePath(string $name): string
55+
{
56+
return sprintf('components/%s.html.twig', str_replace(':', '/', $name));
57+
}
5358
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ 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
198+
name ``form:input`` will look for a template in
199+
``templates/components/form/input.html.twig``.
200+
193201
The mount() Method
194202
~~~~~~~~~~~~~~~~~~
195203

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)