I'm upgrading a CakePHP 1.3 application to CakePHP 2.9.6.
Our ExtendedFormHelper (extends AppHelper) contains an afterRender function which is used to append scripts once the content has been rendered.
In the 1.3 version, all of this works correctly. However, in the 2.x version, the afterRender function gets called before any content is rendered. (checked by putting a die();
in the afterRender function and I just get a blank page)
I also tried with an afterLayout callback but this has the same problem.
I am using a layout file which contains $this->fetch('content')
.
Could anyone point me in the right direction as to why this could be happening? I checked the documentation but as far as I can see, the callback should still work the same.
EDIT:
afterRender function in extended helper:
function afterRender($viewFile) {
if (strlen($this->body) > 0) {
$this->_View->start('afterRenderBody');
echo $this->body;
$this->_View->end();
}
$afterRenderScripts = "";
if (strlen($this->script) > 0) {
$afterRenderScripts .= $this->functions;
$afterRenderScripts .= 'jQuery(document).ready(function ($) {';
if (!empty($this->formId)) {
foreach ($this->formId as $formId) {
$afterRenderScripts .= $this->_View->element('extended_form/validate/after_render_script', array('formId' => $formId, 'rules' => $this->rules));
}
}
$afterRenderScripts .= $this->script;
$afterRenderScripts .= '});';
}
$afterRenderScripts .= "jQuery(document).ready(function($) {
console.log('some extra scripts');
});";
//echo '<script type="text/javascript">' . $afterRenderScripts . "</script>";
$this->Html->scriptBlock($afterRenderScripts, array('block' => 'afterRenderScripts', 'inline' => false));
}
$this->body and $this->script are just 2 variables in my helper.