Plates and zend-view helpers

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.

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.

Hello @ocramius,

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

Thanks in advance,
LowTower.

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

Thanks,

I’ll check that :wink:

Cheers,
LT.

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.

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:

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.

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.

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.

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 :flushed:

Cheers,
LT.

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

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.
I remember it was plates someday, nevertheless!

Cheers,
LT.

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.

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

1 Like

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/
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/

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:

1 Like

Hello @kevinsmith,

thanks a lot for sharing Your solution! :handshake: