2

How can I set template from other module in Zend Framework 2? I have two modules:
- module "A" (It's my main theme)
- module "B" (It's kind of plugin/widget)

I would like to in module "A" set template of module "B".
I tried:

public function viewAction()
{
    // This is action of controller from module A

    $view = new ViewModel();

    $widget = new ViewModel(array('article' => $article));
    $widget->setTemplate('B/content/article'); // <-- Doens't work

    $view->addChild($articleView, 'article');

    return $view;
}

But this exmaple doesn't work. So How can I pass identifier of other module to setTemplate() function? Or maybe there is other function/way to set it?
Thanks for any help

user1409508
  • 623
  • 1
  • 12
  • 38
  • i'm at the same problem right now - did you find any good solution till now? would be grateful if you could share it to us then. – eXe Nov 06 '15 at 08:33

2 Answers2

0

Maybe you need to remap you layout to that what you want, try something like this on your module config:

'view_manager' => array(

    'template_path_stack' => array(
        'MODULE_A' => __DIR__ . '/../view',
    ),

    'template_map' => array(
        'B/content/article'           => __DIR__ . '/../../MODULE_B/view/article/article.phtml',
    ),
),
Fiambre
  • 1,939
  • 13
  • 23
  • 1
    This way it works, but I need more flexible way. Something like 'B' => __DIR__ . '/../../MODULE_B/view/. So I could do: $view->setTemplate('B/module/content/article'). Im doing something like plugins so I dont want to edit my core module("A") and add every view manualy. But thanks anyway. – user1409508 May 07 '14 at 15:59
0

I had a similar problem - created my own module and wanted to render templates from this module without modify configs of the main module.

Seems this Tutorial will answer your question:

http://framework.zend.com/manual/current/en/modules/zend.view.renderer.php-renderer.html

example code:

    $oRenderer = new \Zend\View\Renderer\PhpRenderer();
    $oResolver = new \Zend\View\Resolver\AggregateResolver();
    $oRenderer->setResolver($oResolver);

    $oResolverMap = new \Zend\View\Resolver\TemplateMapResolver(array(
        'yourtemplate' => __DIR__ . '/view/yourtemplate.phtml',
    ));

    $oResolverPath = new \Zend\View\Resolver\TemplatePathStack(array(
        'script_paths' => array(
            __DIR__ . '/view',
        ),
    ));

    $oResolver->attach($oResolverMap);
    $oResolver->attach($oResolverPath);
    $oResolver->attach(new \Zend\View\Resolver\RelativeFallbackResolver($oResolverMap));
    $oResolver->attach(new \Zend\View\Resolver\RelativeFallbackResolver($oResolverPath));

    $oView = new \Zend\View\Model\ViewModel();
    $oView->setTemplate('yourtemplate');
    $sRenderedTemplateSource = $oRenderer->render($oView);
eXe
  • 662
  • 11
  • 26