Skip to content

Instantly share code, notes, and snippets.

@darylteo
Last active November 12, 2019 02:07
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 darylteo/0fb8cc59dbcc62c90d1b81502408b7c4 to your computer and use it in GitHub Desktop.
Save darylteo/0fb8cc59dbcc62c90d1b81502408b7c4 to your computer and use it in GitHub Desktop.
Custom_Controller_Plugin_Layout.php
<?php
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
$layout = Zend_Layout::getMvcInstance();
// Return early if forward detected
if (!$request->isDispatched()
|| $this->getResponse()->isRedirect()
|| ($layout->getMvcSuccessfulActionOnly()
&& (!empty($helper) && !$helper->isActionControllerSuccessful())))
{
return;
}
// Return early if layout has been disabled
if (!$layout->isEnabled()) {
return;
}
if ($layout->getLayout() !== 'layout2018') {
return;
}
$response = $this->getResponse();
$fullContent = null;
$obStartLevel = ob_get_level();
try {
// hack to output header first
$layout->setLayout('layout2018head');
$fullContent = $layout->render();
// set it back to original value for original behaviour.
$layout->setLayout('layout2018');
} catch (Exception $e) {
while (ob_get_level() > $obStartLevel) {
$fullContent .= ob_get_clean();
}
$request->setParam('layoutFullContent', $fullContent);
$request->setParam('layoutContent', $layout->content);
$response->setBody(null);
throw $e;
}
ob_end_flush();
// output header immediately
echo $fullContent;
flush();
}