2013-06-04 16:52:37 - Article

ZF2 different layouts in modules

Content.exe

To enable a different layout in a module, to your module.php for the module define a new layout like so

public function init(ModuleManager $mm)
    {
        $mm->getEventManager()->getSharedManager()->attach(__NAMESPACE__, 'dispatch', function($e) {
            $e->getTarget()->layout('admin/layout');
        });
    }

then within your module.config.php view_manager settings define the template

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

or you can define a direct path in your module.php without any template mapping i.e

public function init(ModuleManager $mm)
    {
        $mm->getEventManager()->getSharedManager()->attach(__NAMESPACE__, 'dispatch', function($e) {
            $e->getTarget()->layout('layout/admin');
        });
    }

This will load module/view/layout/admin.phtml when this module is run

Comments.log - 5 entries

5 responses to “ZF2 different layouts in modules

  1. Zhabba
    Thanks a lot! Pretty clear.
  2. Yoppy Yunhasnawa
    It works, but why the URL from function $this->url() at layout still relative to other module?
    1. Adrian
      Check your routing :)
  3. Azhar
    it gives me an error? Argument 1 passed to Login\Module::init() must be an instance of Login\ModuleManager, instance of Zend\ModuleManager\ModuleManager given, called in C:\xampp\htdocs\mvczend\vendor\zendframework\zendframework\library\Zend\ModuleManager\Listener\InitTrigger.php on line 33 and defined
  4. Venkat
    Thanks a lot !! Easy one and Working fine.

Reply to Zhabba [Cancel Reply]

System Notice: Your email address will not be published. Required fields are marked with *

LOADING...
Initializing CyberDev systems...