CakePHP 2 $ this-> Html & # 8594; script order

I am trying to insert JS files into a view, but they are inserted in the wrong order.

In my default.ctp I have this

$this->Html->script(array(
    'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js',
    'global'
), array('inline'=>false));

echo $this->fetch('script');

In my opinion, I have the following:

$this->Html->script('jquery.fancybox.pack', array('inline' => false));

But when I look at the source, it looks like this:

<script type="text/javascript" src="/js/jquery.fancybox.pack.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="/js/global.js">

This is obviously the wrong order, so the jQuery plugin does not work.

What am I doing wrong?

+5
source share
3 answers

As a rule, I highlight the necessary scripts in the layout (instead of adding them to the buffer), and then the script block (buffered scripts) after. This ensures that the scripts required for each view are echoed first. Instead, your default.ctp will look something like this:

// get echoed immediately
echo $this->Html->script(array(
    'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js',
    'global'
));
// everything else from the view, echoed after
echo $this->fetch('script');

.

echo $this->Html->script(array(
    'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js',
    'global'
), array('block' => 'firstScripts');
echo $this->fetch('css');
echo $this->fetch('firstScripts');
echo $this->fetch('script');
+6

, , , cakePHP .

, , , ( ).

+2

Use this in the chapter section.

Html-> script ('jquery-1.11.1.js')?>
fetch ('script')?>
0
source

All Articles