在“底部”的CakePHP布局中加载Javascript文件

时间:2022-10-20 16:01:55

Currently, I have a big CakePHP application with a layout and a lot of views. In the layout, I load Javascript files in the head which are needed by most views. In the views themself, I either load additional Javascript files (e.g., load a library file that is only needed there), or I add some Javascript code that is only relevant for this view in a script tag, for example if I need a click handler.

目前,我有一个很大的CakePHP应用程序,它有一个布局和很多视图。在布局中,我在头部加载大多数视图所需的Javascript文件。在视图中,我要么加载额外的Javascript文件(例如,加载仅在那里需要的库文件),要么在脚本标记中添加一些仅与此视图相关的Javascript代码,例如,如果我需要单击处理程序。

Since it is a known fact that loading Javascript files in the HTML head blocks loading the page, I wanted to put them at the bottom before closing the body Tag. But if I do so, the Javascript in my views that load the content breaks because it does not know about my loaded Javascript files. I understand that the Javascript code in the loaded views is executed before my files are loaded. But how can I prevent that?

由于已知的事实是在加载页面的HTML头块中加载Javascript文件,我想在关闭正文标记之前将它们放在底部。但是,如果我这样做,我的视图中加载内容的Javascript会中断,因为它不知道我加载的Javascript文件。我知道加载视图中的Javascript代码是在加载文件之前执行的。但是我该如何防止这种情况呢?

I'm currently using the HTML Helper in the Layout (and everywhere else) to load my JS files like this:

我目前正在使用布局中的HTML Helper(以及其他任何地方)加载我的JS文件,如下所示:

<?php echo $this->Html->script('//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'); ?>

And I use the JS Helper to "output" JS at the end of the page with

我使用JS Helper在页面末尾“输出”JS

<?php echo $this->Js->writeBuffer(); ?>

Is there a way I can append my JS code in the views, so that it is executed when I call writeBuffer? Can that help me out?

有没有办法可以在视图中附加我的JS代码,以便在调用writeBuffer时执行它?这可以帮到我吗?

2 个解决方案

#1


12  

This is what I do in my view:

这就是我在我看来做的事情:

echo $this->Html->script('filename', array('inline' => false));

And this is what I do at the bottom of my layout:

这就是我在布局底部所做的事情:

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

#2


18  

Since CakePHP version 2.1 you can use script blocks:

从CakePHP 2.1版开始,您可以使用脚本块:

// in your view
$this->Html->script('filename', array('block' => 'scriptBottom'));

// in your layout
echo $this->fetch('scriptBottom');

This approach lets you keep echo $this->fetch('script') in the <head> of your layout in case you need any scripts at the top.

这种方法允许您在布局的中保持echo $ this-> fetch('script'),以防您需要顶部的任何脚本。

#1


12  

This is what I do in my view:

这就是我在我看来做的事情:

echo $this->Html->script('filename', array('inline' => false));

And this is what I do at the bottom of my layout:

这就是我在布局底部所做的事情:

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

#2


18  

Since CakePHP version 2.1 you can use script blocks:

从CakePHP 2.1版开始,您可以使用脚本块:

// in your view
$this->Html->script('filename', array('block' => 'scriptBottom'));

// in your layout
echo $this->fetch('scriptBottom');

This approach lets you keep echo $this->fetch('script') in the <head> of your layout in case you need any scripts at the top.

这种方法允许您在布局的中保持echo $ this-> fetch('script'),以防您需要顶部的任何脚本。