S
S
Symbi0t2014-10-23 11:23:56
Zend Framework
Symbi0t, 2014-10-23 11:23:56

How to set different templates for different modules in ZF2?

I have a set of different modules. Some are designed for the admin panel, others for the public part of the site.
I need one template for all admin modules, and another for all public part modules.
Inserting a copy of the template into each module is not an option. The inheritance system doesn't help either. How to be?
I'm currently using the following mechanism:

$eventManager->attach('dispatch', array($this, 'setLayout'), -100);
  
    public function setLayout($e)
    {
        $matches    = $e->getRouteMatch();
        $controller = $matches->getParam('controller');
        if (0 !== strpos($controller, __NAMESPACE__, 0)) {
            // not a controller from this module
            return;
        }
 
        // Set the layout template
        $viewModel = $e->getViewModel();
        $viewModel->setTemplate('layout/dashboard');
    }

But because of this, a problem appears, in the form of the inability to disable the template output ( $view->setTerminal(true) )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Symbi0t, 2014-10-23
@Symbi0t

Sorry. Already figured it out.
In the specific module's configuration file ( module.config.php ) add the following:

'view_manager' => array(
        'template_path_stack' => array(
            'lists' => __DIR__ . '/../view',
        ),
  'template_map' => array(
             'adminLayout'   =>  __DIR__ . '/../view/layout/layout.phtml',
        ),
    ),

And in the Module.php file, add the init() method , without first forgetting to include the ModuleManager class :
use Zend\ModuleManager\ModuleManager;
....
public function init(ModuleManager $moduleManager) {
        $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
            $controller = $e->getTarget();
            $controller->layout('adminLayout');
        }, 100);
    }

Source

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question