<commit_before># Setting module-specific Layouts

The following example shows how to set a template for the layout based on a
module name in a zend-mvc based application. The example uses a listener that
listens on the
[`Zend\Mvc\MvcEvent::EVENT_RENDER` event](https://docs.zendframework.com/zend-mvc/mvc-event/#mvceventevent_render-render)
and uses the
[`Zend\Router\RouteMatch` object](https://docs.zendframework.com/zend-mvc/routing/#routing)
to get the called module from the current request.

## Create Listener

Create a listener as a separate class, e.g.
`module/Admin/src/Listener/LayoutListener.php`:

```php
namespace Admin\Listener;

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

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

    public function setLayout(MvcEvent $event) : void
    {
        // Get route match object
        $routeMatch = $event->getRouteMatch();

        // Check route match and parameter for current module
        if ($routeMatch
            && $routeMatch->getParam('module') === 'Admin'
        ) {
            // Get root view model
            $layoutViewModel = $event->getViewModel();

            // Change template
            $layoutViewModel->setTemplate('layout/backend');
        }
    }
}
```

## Register Listener

Extend the module class to register the listener, e.g.
`module/Admin/Module.php`:

```php<commit_msg>Fixes styling in code example<commit_after># Setting module-specific Layouts

The following example shows how to set a template for the layout based on a
module name in a zend-mvc based application. The example uses a listener that
listens on the
[`Zend\Mvc\MvcEvent::EVENT_RENDER` event](https://docs.zendframework.com/zend-mvc/mvc-event/#mvceventevent_render-render)
and uses the
[`Zend\Router\RouteMatch` object](https://docs.zendframework.com/zend-mvc/routing/#routing)
to get the called module from the current request.

## Create Listener

Create a listener as a separate class, e.g.
`module/Admin/src/Listener/LayoutListener.php`:

```php
namespace Admin\Listener;

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

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

    public function setLayout(MvcEvent $event) : void
    {
        // Get route match object
        $routeMatch = $event->getRouteMatch();

        // Check route match and parameter for current module
        if ($routeMatch
            && $routeMatch->getParam('module') === 'Admin'
        ) {
            // Get root view model
            $layoutViewModel = $event->getViewModel();

            // Change template
            $layoutViewModel->setTemplate('layout/backend');
        }
    }
}
```

## Register Listener

Extend the module class to register the listener, e.g.
`module/Admin/Module.php`:

```php
{'fraction_non_alphanumeric': 0.10730121194206325, 'fraction_numerical': 0.0005911912503694946, 'mean_word_length': 2.9952774498229044, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2269174', 'n_tokens_mistral': 970, 'n_tokens_neox': 953, 'n_words': 265}