javascript CakePHP:从视图中在标题中包含 js 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26517487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:03:55  来源:igfitidea点击:

CakePHP: Include js file in header from view

javascriptphpjquerycakephp

提问by Peter

The cakephp docs say:

cakephp 文档说:

By default, script tags are added to the document inline. If you override this by setting $options['inline'] to false, the script tags will instead be added to the script block which you can print elsewhere in the document.

默认情况下,脚本标签被内联添加到文档中。如果您通过将 $options['inline'] 设置为 false 来覆盖它,脚本标签将被添加到您可以在文档中的其他地方打印的脚本块中。

So in my view file (.ctp) I have:

所以在我的视图文件(.ctp)中,我有:

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

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

And in my layout, in the head tag:

在我的布局中,在 head 标签中:

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

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

But the script tag prints out inline and not in the head. If I miss of the echo from the line in my view file, the script doesn't print out at all in my html.

但是脚本标签打印出内联而不是在头部。如果我错过了视图文件中行的回声,则脚本根本不会在我的 html 中打印出来。

Any help would be gratefully received.

任何帮助将不胜感激。

PAE

PAE

回答by ub3rst4r

You have falsein quotes, so PHP is treating it as a stringand not a boolean. It should be:

您有false引号,因此 PHP 将其视为 astring而不是boolean。它应该是:

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

回答by HelloSpeakman

I'd want to expand and mention a couple of things on this.

我想对此进行扩展并提及一些事情。

Inline Script

内联脚本

This will print out the script tag inline which isn't always desired.

这将打印出内联脚本标记,这并不总是需要的。

<?php echo $this->Html->script('script.name'); ?>

Non-Inline Script

非内联脚本

This will place the script where ever you placed $this->fetch('script') in your layout file, usually in the head of your page. (As pointed out by ub3rst4r you were passing false as a string)

这会将脚本放置在您在布局文件中放置 $this->fetch('script') 的位置,通常位于页面的头部。(正如 ub3rst4r 所指出的,您将 false 作为字符串传递)

<?php echo $this->Html->script('script.name', array('inline' => false)); ?>

Block Script

块脚本

This might be a much more useful version for many people, you can place a script block in any layout file (as many as you wish actually). I'll show you an example and call it scriptBottom to go before the end of my body.

对于许多人来说,这可能是一个更有用的版本,您可以在任何布局文件中放置一个脚本块(实际上只要您愿意)。我将向您展示一个示例并将其称为 scriptBottom 以在我的身体结束之前运行。

<?php echo $this->fetch('scriptBottom'); ?>

Then you can pass the block to the script method like such

然后你可以像这样将块传递给脚本方法

<?php $this->Html->script('script.name', array('block' => 'scriptBottom')); ?>

Hope this helps

希望这可以帮助

回答by r3mmel

just put this in your view ctp file. :)

只需将其放在您的视图 ctp 文件中即可。:)

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

回答by Pratik

Why are u going on lot of attributes ?? simply use url

你为什么要使用很多属性?只需使用网址

echo $this->Html->script('http://code.jquery.com/jquery.min.js');

回答by Battousai

In cakephp 3 instead of array('inline' => false) you should use array('block' => true) if anyone is looking for that answer like I was. And you don't need to echo the script at the top of your ctp file you can just place inside php syntax i.e.

在 cakephp 3 而不是 array('inline' => false) 你应该使用 array('block' => true) 如果有人像我一样在寻找那个答案。而且您不需要在 ctp 文件顶部回显脚本,您只需将其放在 php 语法中即可

<?php $this->Html->script('//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', ['block' => true]); ?>

<?php $this->Html->script('//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', ['block' => true]); ?>