0

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.

PDx
  • 1
  • 2
  • Hope 2.x is just an intermediate stage for upgrading to 3.x :) Anyways, I don't remember how `afterRender` behaved in 1.3, it feels like centuries have passed since then, however, rendering != outputting, that's at least true as of 2.x. You may want to share some of your "after render append script" logic, so that people can understand what exactly you're doing there, and tell what's wrong / suggest alternatives. – ndm Apr 04 '17 at 13:52
  • @ndm Hi, thanks for responding! I've included some more information in my question. If I understand you correctly, the problem should lie somewhere else since Cake thinks my page is already rendered before anything has been output? – PDx Apr 04 '17 at 14:36
  • No, what I was trying to say, was that rendering a view doesn't cause any output to be sent (to the client), as the description in your question read as if this were what you'd expect to happen. – ndm Apr 04 '17 at 16:01
  • @ndm Oh ok, I misunderstood :) So am I wrong in thinking that when I echo something in this afterRender callback, it should appear after the rest of my content? This is how it worked in 1.3 atleast, but I can't find anything about how it has changed in 2.x :/ – PDx Apr 05 '17 at 07:07

0 Answers0