0

I have some twig files and inside I use the trans filter, unfortunately I get error like

Unknown "trans" filter.

when I run

bin/console debug:twig --filter trans

I can see the filter there, I see even my custom "trans2" filter, which gives me the same error that the trans2 filter is unknown.

I am running symfony 4 with api-platform installed.

If it helps, other filters like "raw" are working.

My twig.yaml config

twig:
    default_path: '%kernel.project_dir%/src/Resources/views'
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'

My whole symfony is autowired, I initialize twig using autowiring:

public function myAction(...., Twig $twig){
    $template = $twig->load(....);
    // then template render into variable which is sent as email body.
}

I have figured out that if I use $this->container->get("twig") instead of controller action argument, it works. Is there any way to make it somehow work with the controller action argument? I have tried to find some TwigInterface or TemplatingInterface that I thought might work but I did not find any.

Erik Kubica
  • 1,180
  • 3
  • 15
  • 39
  • 1
    does "bin/console debug:container" show anything useful with respect to twig? – Cerad Apr 21 '20 at 14:10
  • 2
    if your action is in an actual controller (extending `AbstractController` / using `ControllerTrait`) you can just use `$this->renderView($template, $params)` - https://github.com/symfony/symfony/blob/43688482bc01c40ec421ff9552e1455df460d3f3/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php#L202 – Jakumi Apr 21 '20 at 14:17
  • depending on the exact version of symfony you're using 4.2 vs. >=4.3, from 4.3 onwards you should use `\Twig\Environment $twig`, before that, you may use `\Symfony\Bundle\TwigBundle\TwigEngine` afaict, the former according to deprecation warning in 4.3: https://github.com/symfony/symfony/blob/4.3/src/Symfony/Bundle/TwigBundle/TwigEngine.php – Jakumi Apr 21 '20 at 14:23
  • On the end of the question I wrote that by getting twig from container it works, its in the end the same result like you all commented. For commands I can use just ContainerAwareCommand and get the twig from container like in the controller. So I guess there is no fancy way to create some config that is applied system wide and it will just work with autowiring, so I will just use the container instance through the app. – Erik Kubica Apr 21 '20 at 19:11

0 Answers0