# Plates and zend-view helpers

**URL:** <https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357>\
**Category:** Mezzio\
**Tags:** zend-form, zend-view\
**Created:** [November 6, 2017, 8:18am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357 "2017-11-06T08:18:51Z")\
**Posts on this page:** 16\
**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:** [November 6, 2017, 8:18am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/1 "2017-11-06T08:18:51Z")

</div>

Hello,

I want to use `plates` as template engine, but would like to use `zend-form` view helpers for rendering the form, maybe `zend-navigation` view helpers as well.  
Is it possible to integrate `zend-view`-helpers (e.g. for rendering a form) into the `plates` template engine in a simple way?  
Or, do I have to create a `plates` extension for each and every view helper, I want to use?

I would appreciate it much if someone could direct me to tutorials or extensive documentation on `plates`!

Thanks in advance,  
LowTower.

---

<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:** [November 6, 2017, 8:44am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/2 "2017-11-06T08:44:56Z")

</div>

You can just add a renderer in front of the `PhpRenderer` instance that multiplexes calls to other renderers, so that it decides on its own what kind of templating engine to use.

---

<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:** [November 9, 2017, 8:01am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/3 "2017-11-09T08:01:55Z")

</div>

Hello @ocramius,

thanks for Your quick reply!  
Could You please be a bit more specific?

Thanks in advance,  
LowTower.

---

<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:** [November 9, 2017, 8:20am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/4 "2017-11-09T08:20:01Z")

</div>

You can probably look at [https://github.com/ZF-Commons/ZfcTwig](https://github.com/ZF-Commons/ZfcTwig) for examples.

---

<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:** [November 9, 2017, 9:38am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/5 "2017-11-09T09:38:07Z")

</div>

Thanks,

I’ll check that 😉

Cheers,  
LT.

---

<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:** [February 1, 2018, 10:14am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/6 "2018-02-01T10:14:04Z")

</div>

Hello @ocramius,

thanks again for Your reply.  
I don’t really understand what’s happening inside `ZfcTwig` because I am not familiar with `ZfcTwig`.  
Maybe You can point me to the essential parts.  
In `ZfcTwig\View\TwigRenderer` I find the methods `__call()` and `plugin()`. I guess the important things happen here. The rest seems to be Twig related stuff.

In short, what I want to do, is use `zend-expressive` and use `plates` for the view part, **but** be able to use the `zend-view` helpers such as `pagination`, `navigation`, `forms` inside plates.

My idea based on Your proposal is to extend `zend-expressive-platesrenderer`.  
I guess, I have to create a new `Zend\View\HelperPluginManager` or at least a factory for it and modify the `Zend\Expressive\Plates\PlatesRenderer(Factory)` (or the `PlatesEngine`) such that it does what You proposed in Your reply.

Is that the right direction?  
Is that, what You meant in Your reply?

Thanks in advance and sorry for my stupidity (still learning),  
Cheers,  
LowTower.

---

<div class="post-metadata">

**Author:** ![xtreamwayz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/xtreamwayz/32/27_2.png) [@xtreamwayz](https://discourse.laminas.dev/u/xtreamwayz)\
**Post date:** [February 2, 2018, 9:05pm UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/7 "2018-02-02T21:05:01Z")

</div>

I have used plates in a project, but I haven’t tried to add zend-view plugins to it. I don’t know if this can be done directly without creating an extension that would act between plates and the zend-view plugins.

A plates extension can be really basic. Have a look at how it’s done for zend-escape:

> <https://github.com/zendframework/zend-expressive-platesrenderer/blob/master/src/Extension/EscaperExtension.php>

I guess you can create one custom zend-view-plugin extension like that and register the plugins you need the same way as in that escape plugin.

---

<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:** [February 6, 2018, 10:34am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/8 "2018-02-06T10:34:55Z")

</div>

Hello @xtreamwayz,

thanks for Your reply.

That would mean something like either a very big extension which takes into account each and every needed `zend-view-helper` or an extra extension for each `zend-view-helper`.  
I was looking for a more elegant way.

Different question:  
You said that You have already used `plates` in different projects so far.  
How do You deal with `navigation`, `form`, `pagination` etc.?  
Do You use plain `html` in the `templates`/`partials`?

Cheers and thanks in advance,  
LowTower.

---

<div class="post-metadata">

**Author:** ![xtreamwayz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/xtreamwayz/32/27_2.png) [@xtreamwayz](https://discourse.laminas.dev/u/xtreamwayz)\
**Post date:** [February 6, 2018, 10:59am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/9 "2018-02-06T10:59:25Z")

</div>

For forms I use html. As long as the forms are not too complicated I find this a lot faster than using zend-form. And for pagination I use a partial to which I pass some basic info and it creates the html. For the top bar navigation I also use plain html. As I don’t need dynamically created routes, it’s good enough for me. But if applications get more complex I need to start looking at those components as well.

---

<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:** [February 6, 2018, 11:24am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/10 "2018-02-06T11:24:00Z")

</div>

Thanks for the info!

Do You know any tutorials on `plates`?  
Information on `plates` is quite rare.  
I wonder, if it is used very much in the wild.  
I wonder, why it is default in `zend-expressive` then 😳

Cheers,  
LT.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [February 6, 2018, 4:02pm UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/11 "2018-02-06T16:02:23Z")

</div>

> [@lowtower](#):
>
> I wonder, why it is default in zend-expressive then…

No default, it’s your choice if you use the installer.

---

<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:** [February 6, 2018, 4:10pm UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/12 "2018-02-06T16:10:09Z")

</div>

You are right.  
Only when using the installer, but the installer has defaults as well!

It seems, it was `zend-view` instead of plates (at least, it was in version 0.4 of `zend-expressive-skeleton`) - see outdated image below.  
It is “nothing” in [release-3.0](https://github.com/zendframework/zend-expressive-skeleton/blob/0924e29291e8887aef7f9c9e49129c51d1c81291/src/ExpressiveInstaller/config.php#L198).  
I remember it was `plates` someday, nevertheless!

Cheers,  
LT.

 ![](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/1X/2ee51449f1fd4faa99608e6f79e973c0d465bbf4.png)

---

<div class="post-metadata">

**Author:** ![xtreamwayz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/xtreamwayz/32/27_2.png) [@xtreamwayz](https://discourse.laminas.dev/u/xtreamwayz)\
**Post date:** [February 6, 2018, 7:16pm UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/13 "2018-02-06T19:16:49Z")

</div>

I think the default was always no template engine. As you can see in that image, the default there is (n) or none of the above. Option 3 was chosen in that image.  
But I think plates was recommended in the docs though as it is fast and lightweight. However if you use Twig with caching enabled it’s also really fast.

---

<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:** [February 7, 2018, 8:14am UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/14 "2018-02-07T08:14:45Z")

</div>

Ahm,  
You’re obviously right.  
I think, I have to get my eyes checked 👓

---

<div class="post-metadata">

**Author:** ![kevinsmith](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@kevinsmith](https://discourse.laminas.dev/u/kevinsmith)\
**Post date:** [February 18, 2018, 3:20pm UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/15 "2018-02-18T15:20:42Z")

</div>

> [@lowtower](#):
>
> possible to integrate zend-view-helpers (e.g. for rendering a form) into the plates template engine in a simple w

Hello, I was looking at the same problem and came up with the following solution.

You can make use of zend view helpers by passing an instance of phprenderer  
into the Plates template view script via an entry in the Plates template data array.

You should have an entry for Zend\View\HelperPluginManager in your config/autoload/templates.global.php  
e.g.

`Zend\View\HelperPluginManager::class => Zend\Expressive\ZendView\HelperPluginManagerFactory::class`

N.B. Zend\View\HelperPluginManager is registered for you if you’ve installed via the skeleton.

Then In your Action Factory:

```
use Zend\View\HelperPluginManager;
use Zend\View\Renderer\PhpRenderer;

$helpers = $container->get(HelperPluginManager::class);
$zvphprenderer = new PhpRenderer;
$zvphprenderer->setHelperPluginManager($helpers);

```

Then In your Action, having passed in $zvphprenderer:

```
$data['zendviewphprenderer'] = $this->zvphprenderer;

//render Plates view (assuming Plates is your default template)
return new HtmlResponse(
       $this->renderer->render('mytemplatesdirectory::mytemplate',$data)
);

```

In your Plates view template you can now make use of various Zend View Helpers  
e.g.

```
<?php $pl = $zendviewphprenderer->placeholder('foo')->set('some random text');?>
<?php echo $zendviewphprenderer->placeholder('foo'); ?>

```

Regarding _zend-form_ view helpers, you will of course need zend-form installed. The cookbook  
documentation at [https://docs.zendframework.com/zend-expressive/cookbook/using-zend-form-view-helpers/](https://docs.zendframework.com/zend-expressive/cookbook/using-zend-form-view-helpers/)  
instructs to use the Zend Form ConfigProvider by adding a zend.form.global.php to  
config/autoload directory (may be set up automatically on install?), replacing  
HelperPluginManagerFactory and changing the entry for it in config/autoload/templates.global.php

However I have found zend form view helpers work without replacing  
HelperPluginManagerFactory (Zend\Expressive\ZendView\HelperPluginManagerFactory) which seems to do the job, as the zend form config provider (zendframework/zend-form/src/ConfigProvider.php)  
returns the form view helpers with a view\_helpers key which  
HelperPluginManagerFactory already checks for to configure the service manager.

Going down the delegator factory route (may not be necesary):

Build the delegator factory as suggested at [https://docs.zendframework.com/zend-expressive/cookbook/using-zend-form-view-helpers/](https://docs.zendframework.com/zend-expressive/cookbook/using-zend-form-view-helpers/)

And as instructed add a ‘delagators’ configuration key to your config/autoload/templates.global.php file.

My config/autoload/templates.global.php file now looks like this:

```
<?php

use Zend\Expressive\Plates\PlatesRendererFactory;
use Zend\Expressive\Template\TemplateRendererInterface;
use Zend\Expressive\ZendView\ZendViewRenderer;
use Zend\Expressive\ZendView\ZendViewRendererFactory;

use Zend\View\HelperPluginManager;

return [
    'dependencies' => [
        'factories' => [
            TemplateRendererInterface::class => PlatesRendererFactory::class,
            ZendViewRenderer::class => ZendViewRendererFactory::class,
            Zend\View\HelperPluginManager::class => Zend\Expressive\ZendView\HelperPluginManagerFactory::class
        ],
        'invokables' => [
        ],
        'delegators' => [
            Zend\View\HelperPluginManager::class => [
                App\FormHelpersDelegatorFactory::class,                          
            ]
        ]
    ],

    'templates' => [
        'extension' => 'phtml',
    ],
];

```

You should now be able to use zend form view helpers in your Plates template  
e.g.

```
<?php
    $form->prepare();
    $form->setAttribute('method', 'post');
    $formLabel = $zendviewphprenderer->plugin('formLabel');
?>

<?php echo $zendviewphprenderer->form()->openTag($form); ?>

```

I created a GIST for it here:

> <https://gist.github.com/kevin2244/5ef80d019b0cc3711d45003a42d9e0fa>
>
> There are more than three files. show original

---

<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:** [February 28, 2018, 4:14pm UTC](https://discourse.laminas.dev/t/plates-and-zend-view-helpers/357/16 "2018-02-28T16:14:28Z")

</div>

Hello @kevinsmith,

thanks a lot for sharing Your solution! 🤝
