title: CakePHP afterRender callback gets called before page is rendered
body: <p>I'm upgrading a CakePHP 1.3 application to CakePHP 2.9.6.</p>
  
  <p>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 <code>die();</code> in the afterRender function and I just get a blank page) </p>
  
  <p>I also tried with an afterLayout callback but this has the same problem.</p>
  
  <p>I am using a layout file which contains <code>$this-&gt;fetch('content')</code>.</p>
  
  <p>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.</p>
  
  <p>EDIT:</p>
  
  <p>afterRender function in extended helper:</p>
  
  <pre><code>function afterRender($viewFile) {
      if (strlen($this-&gt;body) &gt; 0) {
          $this-&gt;_View-&gt;start('afterRenderBody');
          echo $this-&gt;body;
          $this-&gt;_View-&gt;end();
      }
  
      $afterRenderScripts = "";
  
      if (strlen($this-&gt;script) &gt; 0) {
          $afterRenderScripts .= $this-&gt;functions;
          $afterRenderScripts .= 'jQuery(document).ready(function ($) {';
          if (!empty($this-&gt;formId)) {
              foreach ($this-&gt;formId as $formId) {
                  $afterRenderScripts .= $this-&gt;_View-&gt;element('extended_form/validate/after_render_script', array('formId' =&gt; $formId, 'rules' =&gt; $this-&gt;rules));
              }
          }
          $afterRenderScripts .= $this-&gt;script;
          $afterRenderScripts .= '});';
      }
  
  
      $afterRenderScripts .= "jQuery(document).ready(function($) {
  console.log('some extra scripts');
  });";
  
      //echo '&lt;script type="text/javascript"&gt;' .  $afterRenderScripts . "&lt;/script&gt;";
      $this-&gt;Html-&gt;scriptBlock($afterRenderScripts, array('block' =&gt; 'afterRenderScripts', 'inline' =&gt; false));
  }
  </code></pre>
  
  <p>$this->body and $this->script are just 2 variables in my helper.</p>