Partial in Mezzio


normally in Laminas MVC I use layout helper partial in my templates to separate the header or footer code. However in mezzio this option doesn’t work. How can I add some code from separate .phtml files to the main layout?

Thanks for answers.

Define a namespace for your partials in the configuration. This can be done via a config provider class or configuration file.

Example for config provider class, e.g. src/App/ConfigProvider.php

public function getTemplates(): array
    return [
        'paths' => [
            'app'      => ['templates/app'],
            'error'    => ['templates/error'],
            'layout'   => ['templates/layout'],
            'partials' => ['templates/partials'],

Create a view script, e.g. src/App/templates/partials/example.phtml:


 * @var Laminas\View\Renderer\PhpRenderer $this
 * @var string                            $headline

<h1><?= $headline ?></h1>

Then use the partial view helper in your layout script or in any view script:

<?= $this->partial('partials::example', ['headline' => 'Example']) ?>


Another option is to use PHP’s include expression then no extra rendering by laminas-view is needed.

Example for a layout script:

$headline = 'Example';

include __DIR__ . '/../partials/example.phtml';

Thank you for your help :slight_smile: