Answer the question
In order to leave comments, you need to log in
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');
}
Answer the question
In order to leave comments, you need to log in
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',
),
),
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);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question