Including jQuery scripts just before the closing body HTML tag in CakePHP 2.2.2

Written by James McDonald

September 24, 2012

In your view file app/View/Controller/view.ctp or at the top of your layout app/View/Layouts/layout.ctp put

$this->Html->script(
    'jquery.min', 
    array(
        'inline' => false, 
        'block' => 'script_bottom'
    )
);
# or multiple scripts
$this->Html->script( array(
         'jquery.min', 
         'slider'
    ), 
    array(
        'inline' => false, 
        'block' => 'script_bottom'
    )
)

In your layout file app/View/Layouts/default.ctp

	</div>
	<?php echo $this->element('sql_dump');
		echo $this->fetch('script_bottom'); ?>
</body>
</html>

Outputs something like this:

<script type="text/javascript" src="/dev/cakephp-nodb/js/arbitrary_script_name.js"></script></body>
</html>

The script or scripts needs to be copied into app/webroot/js

script_bottom is a block name that you make up.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Clear HSTS Settings in CHrome

Open chrome://net-internals/#hsts enter the domain in the query field and click Query to confirm it has HSTS settings...

Ubuntu on Hyper-v

It boils town to installing linux-azure # as root or sudo apt-get update apt-get install linux-azure...