How to append scripts to page in afterRender callback

Hi

I’m upgrading a CakePHP application from 1.3 to 2.9.
I’m trying to append scripts to the bottom of my page.

In the 1.3 version, I could just echo javascript between script tags in the afterRender function of our extendedformhelper.
In the 2.9 version, this doesn’t work anymore. The javascript gets added to the very top of the page (so before anything has been rendered I assume?).
Is this how the afterRender function is supposed to work in cake 2?

I’m now using script blocks to output the scripts to the bottom of my page, which works. But unfortunately I can’t append anything to this block after the page has been output. So when I call a function to render an input field from my extendedformhelper, the afterRender function does get called but no scripts get added to the page.

Is there a new/better way of doing this?

TL;DR: how to append script tags to bottom of page after a view (including elements) has been rendered?

This is the code that I’m currently using in the afterRender function:
$this->Html->scriptBlock($afterRenderScripts, array(‘block’ => ‘afterRenderScripts’, ‘inline’ => false));