Zend Expressive, twig extensions

I use simple twig extensions and it works:

'extensions' => [
    new App\Template\DebugTwigFunction(),
    new App\Template\AssetTwigFilter(getPublicPath(), isDevelopmentMode()),

However, sometimes I need to take data from my request after run middleware for initialize extension.
I will try to tell you more.

I create a client for api. In middleware, I make a request for my api and get a language translation list.

After, I want create a pretty filter to look at the translations from the view.

{{ 'my_trans_key' | trans }}

So, I will need some TransTwigFilter that should know about the translations that were received in the middleware.

Is it possible to realize this?

In this case, you’d probably pass the dependencies needed for translation lookups to the constructor of your twig extension:

$extensions = [

// …

new MyTranslationFilter($myRepository),

// …


It is not that simple.
The data that I need is already received in the middleware.

I will try to use this example:

But it does not always work.

Can you see this demo. May be it’s you want?

Receive lang attr, render and respond in the handler.

$data = ['lang' => $request->getAttribute('lang')];
return new HtmlResponse($this->template->render('app::home', $data));

Then, if you want to use more the symfony twig extensions, that you can try this component zfegg/symfony-twig-factory.