CakePHP 2 $ this-> HTML->脚本顺序 [英] CakePHP 2 $this->Html->script order

查看:87
本文介绍了CakePHP 2 $ this-> HTML->脚本顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将JS文件插入到视图中,但是它们以错误的顺序插入。

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

在我的default.ctp文件中,我有这个

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 view I have this:

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

但是当我查看源代码时,它像这样出现:

But when I view the source it comes out 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">

显然这是错误的顺序,因此jQuery插件无法正常工作。

Which is obviously the wrong order so the jQuery plugin is not working.

我在做什么错了?

推荐答案

通常,我会回显布局中所需的脚本(而不是将它们添加到缓冲区中),然后再添加脚本块(缓冲脚本)。这样可以确保首先回显每个视图所需的脚本。您的default.ctp看起来像这样:

Generally, I echo out the required scripts in the layout (instead of adding them to the buffer) and then script block (buffered scripts) after. This ensures that scripts required for each view are echoed first. Your default.ctp would look something like this instead:

// 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');

或者,您可以为前面的脚本指定一个特殊的块。

Or, you can specify a special block for your preceding scripts.

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');

这篇关于CakePHP 2 $ this-&gt; HTML-&gt;脚本顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆