26

I am learning ZF2.0 beta. In ZF1 we can change layout on the fly in controller:

Zend_Layout::getMvcInstance()->setLayout('layoutname');

But in ZF2.0 Zend\Layout class doesn't has method getMvcInstance(). I think that it can be made using dependency injections but not sure.

5 Answers 5

57

The ZF2 is heavily under development and no guarantee can be made the way it works now, will be the way it works when ZF2 reaches a stable state.

However, the new view layer from Zend\Mvc is recently merged so you should be able to do this now (with current master):

public function somethingAction () 
{
    // Do some intelligent work

    $this->layout('layout/different');
}
1
  • 1
    I am trying to use this approach, but I don't get the result. Layout is still standart (layout/layout). Why it doesn't work? You can read more about my problem here. Commented May 16, 2014 at 19:26
8
public function someAction() {
    $layout = $this->layout();
    $layout->setTemplate('layout/custom');
    $viewModel = new ViewModel();
    return $viewModel;
}
2
  • Perfect solution for multiple layouts when changing existing Layouts
    – temp-learn
    Commented Sep 5, 2013 at 23:42
  • It looks like working, but id doesn't work for me :( image which shows my code. I don't know why it doesn't work :'( Commented May 15, 2014 at 22:48
7

The best way I've found to set templates in actions is like this

public function someAction() {
    $viewModel = new ViewModel();
    $viewModel->setTemplate('layout/custom');

    return $viewModel;
}

In your module.config.php make sure you've set your appropriate template_map path.

    'view_manager' => array(
    'template_map' => array(
        'layout/custom' => __DIR__ . '/../view/layout/custom.phtml'
    ),
),
1
  • 7
    This will set view, but not layout, as author asked. Commented May 16, 2014 at 18:59
3

You will also have to set the layout either in the bootstrap or when using di. Di example:

    'Zend\View\Resolver\TemplateMapResolver' => array(
        'parameters' => array(
            'map'  => array(
            'layout/different' => __DIR__ . '/../view/layout/different.phtml',
            ),
        ),
    ),
3

I have tried the above tips.

public function somethingAction () 
{
    // Do some intelligent work

    $this->layout('layout/different');
}

I got the correct result with this snippet.

public function someAction() {
    $viewModel = new ViewModel();
    $viewModel->setTemplate('layout/custom');

    return $viewModel;
}

It fetched both layouts(default & current module).

2
  • I encountered the same result. I think it's important that we differentiate between layout and template. For changing the layout, we should be using $this->layout('layout/different'); instead of the setTemplate function. Commented Mar 5, 2014 at 5:40
  • Code in your snippet will set what view will be rendered, not layout. Commented May 15, 2014 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.