Last active
December 19, 2015 10:48
-
-
Save joshribakoff/5942631 to your computer and use it in GitHub Desktop.
Assign widget to ZF2 layout with events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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