# Zend Expressive, twig extensions

**URL:** <https://discourse.laminas.dev/t/zend-expressive-twig-extensions/969>\
**Category:** Mezzio\
**Tags:** twig\
**Created:** [March 1, 2019, 12:29pm UTC](https://discourse.laminas.dev/t/zend-expressive-twig-extensions/969 "2019-03-01T12:29:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nepster-web](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/nepster-web/32/432_2.png) [@nepster-web](https://discourse.laminas.dev/u/nepster-web)\
**Post date:** [March 1, 2019, 12:29pm UTC](https://discourse.laminas.dev/t/zend-expressive-twig-extensions/969/1 "2019-03-01T12:29:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [March 5, 2019, 8:01am UTC](https://discourse.laminas.dev/t/zend-expressive-twig-extensions/969/2 "2019-03-05T08:01:44Z")

</div>

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

$extensions = [

// …

new MyTranslationFilter($myRepository),

// …

];

---

<div class="post-metadata">

**Author:** ![nepster-web](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/nepster-web/32/432_2.png) [@nepster-web](https://discourse.laminas.dev/u/nepster-web)\
**Post date:** [March 5, 2019, 4:45pm UTC](https://discourse.laminas.dev/t/zend-expressive-twig-extensions/969/3 "2019-03-05T16:45:10Z")

</div>

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

I will try to use this example:  
[https://discourse.zendframework.com/t/how-can-i-get-serverrequestinterface-with-attributes-in-factory/953/6?u=nepster-web](https://discourse.zendframework.com/t/how-can-i-get-serverrequestinterface-with-attributes-in-factory/953/6?u=nepster-web)

But it does not always work.

---

<div class="post-metadata">

**Author:** ![Moln](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/moln/32/324_2.png) [@Moln](https://discourse.laminas.dev/u/Moln)\
**Post date:** [March 8, 2019, 8:59am UTC](https://discourse.laminas.dev/t/zend-expressive-twig-extensions/969/4 "2019-03-08T08:59:20Z")

</div>

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

> **[zfegg/zf-application-tutorials](https://github.com/zfegg/zf-application-tutorials/tree/expressive/example/twig-translation-extension-demo)**
>
> Zend Framework 3 框架应用教学. Contribute to zfegg/zf-application-tutorials development by creating an account on GitHub.

Receive `lang` attr, render and respond in the handler.

```php
$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](https://github.com/zfegg/symfony-twig-factory).
