# Zend-Expressive and Plates

**URL:** <https://discourse.laminas.dev/t/zend-expressive-and-plates/260>\
**Category:** Mezzio\
**Tags:** expressive\
**Created:** [August 23, 2017, 4:24pm UTC](https://discourse.laminas.dev/t/zend-expressive-and-plates/260 "2017-08-23T16:24:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lowtower](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/lowtower/32/98_2.png) [@lowtower](https://discourse.laminas.dev/u/lowtower)\
**Post date:** [August 23, 2017, 4:24pm UTC](https://discourse.laminas.dev/t/zend-expressive-and-plates/260/1 "2017-08-23T16:24:23Z")

</div>

In the `zend-expressive-skeleton`, the template engine `plates` is the default.  
Unfortunately no tutorials or how-tos exist in the wild.  
How is it possible to enable plates extensions in `zend-expressive`, such as the [uri-extension](http://platesphp.com/extensions/uri/)?  
Well, I can add to configuration, e.g. templates.global.php:

```php
     'plates' => [
         'extensions' => [
             \League\Plates\Extension\URI::class,
         ],
     ],

```

But how to pass the required extra parameter?

Thanks in advance,  
LT.

---

<div class="post-metadata">

**Author:** ![harikt](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/harikt/32/47_2.png) [@harikt](https://discourse.laminas.dev/u/harikt)\
**Post date:** [August 23, 2017, 4:40pm UTC](https://discourse.laminas.dev/t/zend-expressive-and-plates/260/2 "2017-08-23T16:40:13Z")

</div>

Set `PlatesEngine::class` to container.

> <https://github.com/zendframework/zend-expressive-platesrenderer/blob/6d23d4638de2282bb982362950f1a1d02ca6177b/src/PlatesRendererFactory.php#L83>

Create a middleware and pass `PlatesEngine::class` and needed values ( extensions etc ? ) to the constructor and that can register the extension.

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [August 23, 2017, 5:07pm UTC](https://discourse.laminas.dev/t/zend-expressive-and-plates/260/3 "2017-08-23T17:07:52Z")

</div>

> [@lowtower](#):
>
> How is it possible to enable plates extensions in zend-expressive, such as the uri-extension?

First, the Plates integration for Expressive ships with its own extensions for handling URIs ([view source](https://github.com/zendframework/zend-expressive-platesrenderer/blob/master/src/Extension/UrlExtension.php) — it registers both a `url()` and a `serverurl()` function) which is engineered to work with whatever zend-expressive-router implementation you are using; you should definitely use this in most cases.

Second, as @harikt notes, you could manipulate the `PlatesEngine` within middleware.

Third, as you noted, you can provide configuration via the `plates.extensions` key. These can be any of:

- instances
- directly instantiable class names
- service names

If the extension needs arguments in order to work, create a factory for the extension, register that factory for the extension name, and then use the service name of the extension in your configuration.

The last solution is the best one if your extension needs arguments, as it ensures that the factory is not called unless the middleware being dispatched in the current request needs a renderer.

However, if the extension relies on information that is based on the _request_, the middleware path is likely the best solution. If you absolutely require that you use the `uri()` extension, for instance, I’d write middleware like the following:

```php
function (ServerRequestInterface $request, DelegateInterface $delegate) use (PlatesEngine $engine) {
    $extension = new URI($request->getUri()->getPath());
    $engine->loadExtension($extension);
    return $delegate->process($request);
}

```

Note that I’m pulling the path information _from the request_, and not a superglobal; this is more testable, and ensures that the path provided is the same that the request is using, regardless of the web server being used.

---

<div class="post-metadata">

**Author:** ![lowtower](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/lowtower/32/98_2.png) [@lowtower](https://discourse.laminas.dev/u/lowtower)\
**Post date:** [August 24, 2017, 7:46am UTC](https://discourse.laminas.dev/t/zend-expressive-and-plates/260/4 "2017-08-24T07:46:00Z")

</div>

Thanks a lot for the quick reply!  
This is a very valuable platform for me!  
😀
