Skip to content

Commit 94d8600

Browse files
committed
Allow using directory name as component name for anonymous components
1 parent 4652e51 commit 94d8600

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,47 @@ the subdirectory:
676676
{# renders as: #}
677677
<button class="primary">Click Me!</button>
678678

679+
If your component lives in a directory with the same name, you can
680+
name the component file ``index.html.twig`` to avoid repetition:
681+
682+
.. code-block:: html+twig
683+
684+
{# templates/components/Menu/index.html.twig #}
685+
...
686+
<nav {{ attributes.defaults({class: 'navbar'}) }}>
687+
<ul>
688+
{% block content %}{% endblock %}
689+
</ul>
690+
</nav>
691+
692+
.. code-block:: html+twig
693+
694+
{# templates/components/Menu/Item.html.twig #}
695+
...
696+
{% props href %}
697+
<li {{ attributes.defaults({class: 'navitem'}) }}>
698+
<a href="{{ href }}">{% block content %}{% endblock %}</a>
699+
</li>
700+
701+
.. code-block:: html+twig
702+
703+
{# index.html.twig #}
704+
...
705+
<div>
706+
<twig:Menu>
707+
<twig:Menu:Item href="/">Home</twig:Menu:Item>
708+
<twig:Menu:Item href="/about">About</twig:Menu:Item>
709+
</twig:Menu>
710+
</div>
711+
712+
{# renders as: #}
713+
<nav class="navbar">
714+
<ul>
715+
<li class="navitem"><a href="/">Home</a></li>
716+
<li class="navitem"><a href="/about">About</a></li>
717+
</ul>
718+
</nav>
719+
679720
Like normal, you can pass extra attributes that will be rendered on the element:
680721

681722
.. code-block:: html+twig

src/TwigComponent/src/ComponentTemplateFinder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public function findAnonymousComponentTemplate(string $name): ?string
6666
return $template;
6767
}
6868

69+
$template = rtrim($this->directory, '/').'/'.$componentPath.'/index.html.twig';
70+
if ($loader->exists($template)) {
71+
return $template;
72+
}
73+
6974
$parts = explode('/', $componentPath, 2);
7075
if (\count($parts) < 2) {
7176
return null;

src/TwigComponent/tests/Unit/ComponentTemplateFinderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,18 @@ public function testFindTemplateWithinDirectory()
117117
'foo/bar.html.twig',
118118
'bar/foo/bar.html.twig',
119119
'foo/foo/bar.html.twig',
120+
'foo/qux/index.html.twig',
121+
'foo/foo/baz/index.html.twig',
120122
];
121123
$loader = $this->createLoader($templates);
122124
$finder = new ComponentTemplateFinder($loader, 'foo');
123125

124126
$this->assertEquals('foo/bar.html.twig', $finder->findAnonymousComponentTemplate('bar'));
125127
$this->assertEquals('foo/foo/bar.html.twig', $finder->findAnonymousComponentTemplate('foo:bar'));
126128
$this->assertEquals('foo/foo/bar.html.twig', $finder->findAnonymousComponentTemplate('foo:bar'));
129+
130+
$this->assertEquals('foo/qux/index.html.twig', $finder->findAnonymousComponentTemplate('qux'));
131+
$this->assertEquals('foo/foo/baz/index.html.twig', $finder->findAnonymousComponentTemplate('foo:baz'));
127132
}
128133

129134
private function createLoader(array $templates): LoaderInterface

0 commit comments

Comments
 (0)