# How to include module's .phtml from layout?

**URL:** https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704
**Category:** Components & MVC
**Tags:** zend-view
**Created:** [August 17, 2018, 8:26am UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704 "2018-08-17T08:26:10Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![dronord](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@dronord](https://discourse.laminas.dev/u/dronord)
#### Post date: [August 17, 2018, 8:26am UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/1 "2018-08-17T08:26:10Z")

</div>

Hello.

Idea:

```
<?php // layout.phtml

...

<body>

<?= $this->partial('sidebar') ?>

<?= $this->content ?>

</body>

?>

```

Problem: partial() does not work.

How to include static .phtml from current module directory?  
Zend Framework 3.

---

<div class="post-metadata">

### Author: ![tasmaniski](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tasmaniski/32/70_2.png) [@tasmaniski](https://discourse.laminas.dev/u/tasmaniski)
#### Post date: [August 17, 2018, 2:17pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/2 "2018-08-17T14:17:28Z")

</div>

The partial actually works, this is from my codebase:

```
somewhere in the layout.phtml
<?= $this->partial('partial/js_translations.tpl.php'); ?>
```

---

<div class="post-metadata">

### Author: ![dronord](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@dronord](https://discourse.laminas.dev/u/dronord)
#### Post date: [August 17, 2018, 3:05pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/3 "2018-08-17T15:05:31Z")

</div>

Works for Application, not modules.

---

<div class="post-metadata">

### Author: ![tasmaniski](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tasmaniski/32/70_2.png) [@tasmaniski](https://discourse.laminas.dev/u/tasmaniski)
#### Post date: [August 17, 2018, 3:37pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/4 "2018-08-17T15:37:21Z")

</div>

Try following.  
Add in module’s config key to the partial file

```
'view_manager' => [
    'template_path_stack' => [
        __DIR__. '/../view',
    ],
    'template_map' => [
        'partial-key' => __DIR__. '/../view/module/partial/partial.phtml',
    ],
],

```

and than in layout.phtml

```
<?= $this->partial('partial-key'); ?>
```

---

<div class="post-metadata">

### Author: ![dronord](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@dronord](https://discourse.laminas.dev/u/dronord)
#### Post date: [August 17, 2018, 3:48pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/5 "2018-08-17T15:48:27Z")

</div>

I predict this will only work for the last module in the global /config/modules.config.php. I want each module to have a separate partial.phtml.

---

<div class="post-metadata">

### Author: ![tasmaniski](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tasmaniski/32/70_2.png) [@tasmaniski](https://discourse.laminas.dev/u/tasmaniski)
#### Post date: [August 17, 2018, 3:50pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/6 "2018-08-17T15:50:18Z")

</div>

Actually it will be merged, so just add different key in every config array.

---

<div class="post-metadata">

### Author: ![dronord](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@dronord](https://discourse.laminas.dev/u/dronord)
#### Post date: [August 17, 2018, 5:00pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/7 "2018-08-17T17:00:43Z")

</div>

I will be compelled to pass it in each method of the controller? is there an easy way to get the name of the current module in the layout?

---

<div class="post-metadata">

### Author: ![tasmaniski](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tasmaniski/32/70_2.png) [@tasmaniski](https://discourse.laminas.dev/u/tasmaniski)
#### Post date: [August 17, 2018, 5:20pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/8 "2018-08-17T17:20:51Z")

</div>

Yes there is 🙂 I created a module for that [zend-current-route](https://github.com/tasmaniski/zend-current-route)  
so you can just install it and use:

```
$this->currentRoute()->getModule(); // return current module name
```

---

<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: [August 17, 2018, 6:48pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/9 "2018-08-17T18:48:42Z")

</div>

> [@dronord](#):
>
> I will be compelled to pass it in each method of the controller?

**Never!**

An option is a listener. Short example:

Listener:

```php
namespace Application\Listener;

use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;

class MyListener extends AbstractListenerAggregate
{
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $this->listeners['addSidebar'] = $events->attach(
            MvcEvent::EVENT_RENDER,
            [
                $this,
                'addSidebar',
            ]
        );
    }

    public function addSidebar(MvcEvent $event)
    {
        $serviceManager = $event->getApplication()->getServiceManager();

        /** @var \Zend\Mvc\View\Http\ViewManager $viewManager */
        $viewManager = $serviceManager->get('ViewManager');
        $viewManager->getViewModel()->addChild(
            (new \Zend\View\Model\ViewModel(
                [
                    'foo' => 'bar',
                ]
            ))->setTemplate('sidebar'),
            'sidebar'
        );
    }
}

```

Add to your module config:

```php
'service_manager' => [
    'factories' => [
        // ...
        Application\Listener\MyListener::class => Zend\ServiceManager\Factory\InvokableFactory::class,
    ],
],

```

Add to your module class:

```php
public function onBootstrap(EventInterface $e)
{
    $application = $e->getApplication();

    $aggregate = $application->getServiceManager()->get(
        MyListener::class
    );
    $aggregate->attach($application->getEventManager());

    return [];
}

```

Create the `sidebar.phtml` and add in your layout:

```php
<?= $this->sidebar ?>

```

---

<div class="post-metadata">

### Author: ![dronord](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@dronord](https://discourse.laminas.dev/u/dronord)
#### Post date: [August 17, 2018, 6:49pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/10 "2018-08-17T18:49:59Z")

</div>

Not bad.  
May be need zend-module-config? Where module’s settings will overlap Application.

---

<div class="post-metadata">

### Author: ![dronord](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@dronord](https://discourse.laminas.dev/u/dronord)
#### Post date: [August 17, 2018, 7:35pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/11 "2018-08-17T19:35:57Z")

</div>

Thank you!  
Don’t you think that’s too much?

---

<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: [August 17, 2018, 7:50pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/12 "2018-08-17T19:50:23Z")

</div>

> [@dronord](#):
>
> Don’t you think that’s too much?

It’s your choice. You can copy the same code in every controller or create **one** listener. 😉

With this type of listener you can also create a guard that verifies user access, you can set ACL to the navigation view helpers and so on.

Btw. this is only one idea / suggestion.

---

<div class="post-metadata">

### Author: ![dronord](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@dronord](https://discourse.laminas.dev/u/dronord)
#### Post date: [August 24, 2018, 7:03pm UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/13 "2018-08-24T19:03:16Z")

</div>

I cant understand, view helper have not access to Request object?

---

<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: [August 25, 2018, 8:40am UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/14 "2018-08-25T08:40:37Z")

</div>

This is _not_ a view helper, it is a listener which can be added in a mvc-based application. More infos can be found in the [“mvc event” section](https://docs.zendframework.com/zend-mvc/mvc-event/) of the documentation.

Btw. you can copy the code, add to your application and test it yourself.

---

<div class="post-metadata">

### Author: ![dronord](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@dronord](https://discourse.laminas.dev/u/dronord)
#### Post date: [August 27, 2018, 9:22am UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/15 "2018-08-27T09:22:30Z")

</div>

About listener i understand.  
Is it possible to solve this problem by view helper?  
“Display” task by view way.  
Thank you!

---

<div class="post-metadata">

### Author: ![tasmaniski](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tasmaniski/32/70_2.png) [@tasmaniski](https://discourse.laminas.dev/u/tasmaniski)
#### Post date: [August 27, 2018, 9:56am UTC](https://discourse.laminas.dev/t/how-to-include-modules-phtml-from-layout/704/16 "2018-08-27T09:56:02Z")

</div>

> I cant understand, view helper have not access to Request object?

Yes it has.

You can use module that I sent you - just “plug & play it”.

I certainly encourage you to take a look at the code.  
In the CurrentRouteFactory.php you can see

```
$routeMatch = $container->get('Application')->getMvcEvent()->getRouteMatch();

```

this is what you need
