Skip to content

Instantly share code, notes, and snippets.

@joshribakoff
Last active December 19, 2015 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshribakoff/5942631 to your computer and use it in GitHub Desktop.
Save joshribakoff/5942631 to your computer and use it in GitHub Desktop.
Assign widget to ZF2 layout with events
<?php
public function onBootstrap(MvcEvent $e)
{
/** add categories to sidebar on every request */
$e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_RENDER, function() use($e) {
$sm = $e->getApplication()->getServiceManager();
/** Only add it if the layout actually has a left column */
if('layout/layout-2col-left.phtml' == $e->getViewModel()->getTemplate()) {
/** Render it out to the sidebar in the layout */
$sidebar = new ViewModel(array(
'foo'=>'bar'
));
$sidebar->setTemplate('layout/categories');
$htmlOutput = $sm->get('viewrenderer')
->render($sidebar);
$e->getViewModel()->sidebar .= $htmlOutput;
$e->getViewModel()->sidebar .= 'I am on the sidebar too!';
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment