简体   繁体   English

如何在 zend 框架中附加脚本文件

[英]how to append script file in zend framework

My layouts are placed inside layout/scripts/layout.phtml i have placed the below code inside my head section of layout.phtml我的布局放在 layout/scripts/layout.phtml 里面我已经把下面的代码放在 layout.phtml 的 head 部分

<?php
print $this->headScript()->appendFile($this->baseUrl().'/js/jquery-1.7.2.min.js')
                         ->appendFile($this->baseUrl().'/js/simpla.jquery.configuration.js');
?>

Now I want to append another javascript file from a view.现在我想从视图中附加另一个 javascript 文件。 For that I wrote the following code:为此,我编写了以下代码:

 $this->headScript()->appendFile($this->baseUrl().'js/fancybox/jquery.fancybox-1.3.4.pack.js');

Although this appended the file but it appears before my jquery-1.7.2.min.js.虽然这附加了文件,但它出现在我的 jquery-1.7.2.min.js 之前。 What i want is that i want to add jquery.fancybox-1.3.4.pack.js below my jquery-1.7.2.min.js How can i do this?我想要的是我想在我的 jquery-1.7.2.min.js 下面添加 jquery.fancybox-1.3.4.pack.js 我该怎么做?

Your view script is rendered before the layout so the calls to appendFile() in your layout result in those scripts (jquery-1.7.2 and simpla.jquery) being appended after the one you appended in the view script.您的视图脚本在布局之前呈现,因此布局中对appendFile()的调用导致这些脚本(jquery-1.7.2 和 simpla.jquery)被附加在您在视图脚本中附加的脚本之后。

To fix this, use prependFile() in your layout at least for the main jQuery script.要解决此问题,请至少在主 jQuery 脚本的布局中使用prependFile()

Your layout might look like this:您的布局可能如下所示:

<?php
print $this->headScript()
           ->appendFile($this->baseUrl().'/js/simpla.jquery.configuration.js')
           ->prependFile($this->baseUrl().'/js/jquery-1.7.2.min.js');

No need to change the view script, that is fine as is.无需更改视图脚本,这很好。

See HeadScript Helper Example #23 which talks a bit about ordering of the scripts.请参阅HeadScript Helper Example #23,其中讨论了脚本的排序。

The important thing to remember that they don't mention is that your view script gets rendered before the layout does.重要的是要记住他们没有提到的是你的视图脚本在布局之前被渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM