Search code examples
symfonytwigbundle

How to use Symfony TwigComponent from bundle


I am using symfony 5.4.

I have a bundle located in my vendor folder that works fine.

I have created a LiveTwigComponent.

It works perfectly when the template file 'network_list.html.twig' is located in my 'templates/components' folder. As the logic belongs to my Bundle, i have tried to move it to the 'src/Resources/views/components' folder. If I do so, I have an error: "Unable to find template "components/network_list.html.twig" In my template, I call the component like this

{{ component('network_list') }}

I have tried to prefix with the bundle namespace as it works for the other templates I have in my bundle

{{ component('@MyFooBarBundle\\network_list') }}

and

{{ component('@MyFooBarBundle\\components\\network_list') }}

but doesn't work either, I have another error message

An exception has been thrown during the rendering of a template ("Unknown component "@MyFooBarBundle\components\network_list". The registered components are: network_list").

I suspect I may have to add a config line for twig to tell the bundle to look into my bundle subfolder and i didn't find any documentation about this.

What am i doing wrong ?

Thanks in advance


Solution

  • You can customize the template used to render the component by passing it as the second argument to the AsTwigComponent attribute:

    - #[AsTwigComponent('network_list')]
    + #[AsTwigComponent('network_list', template: '@MyFooBarBundle/network_list.html.twig')]
      class NetworkListComponent
      {
          // ...
      }