CakePHP 如何从元素内部添加Script()?

发布于 2024-10-12 18:22:04 字数 961 浏览 6 评论 0原文

我在 CakePHP element 文件 (views/elements/nav_default.ctp) 中有一个导航菜单。

该文件包含在另一个元素中,即标题 (views/elements/header_default.ctp),然后包含在布局文件 (views/layouts/default.ctp) 中。

我试图告诉 Cake 从 nav 元素中加载 js 文件(webroot/js/mega_drop.js),如下所示:

<?php
$this->addScript('mega_drop');
?>

它不被包含在内。我查看了 addScript 的文档,其中只是说:

将内容添加到内部脚本中 缓冲。该缓冲区可用 在布局中作为 $scripts_for_layout。 这个方法在创建的时候很有用 需要添加 javascript 的帮助程序或 css 直接布局。留在 请注意,从添加的脚本 布局,或布局中的元素将 不添加到 $scripts_for_layout。 此方法最常用于 内部助手,如 Javascript 和 Html 助手。

关键部分:

请记住,从布局添加的脚本或布局中的元素不会添加到 $scripts_for_layout。

那我该怎么办呢?

我想我可以将 添加到 default.ctp 布局中。但这感觉不太对劲,因为它将布局和元素紧密地联系在一起。

CakePHP 的最佳实践方法是什么?

I have a navigation menu inside a CakePHP element file (views/elements/nav_default.ctp).

The file is included inside another element that is the header (views/elements/header_default.ctp) which is then included in the layout file (views/layouts/default.ctp).

I am trying to tell Cake to load a js file (webroot/js/mega_drop.js) from within the nav element like so:

<?php
$this->addScript('mega_drop');
?>

It does not get included. I looked at the documentation for addScript which just says:

Adds content to the internal scripts
buffer. This buffer is made available
in the layout as $scripts_for_layout.
This method is helpful when creating
helpers that need to add javascript or
css directly to the layout. Keep in
mind that scripts added from the
layout, or elements in the layout will
not be added to $scripts_for_layout.
This method is most often used from
inside helpers, like the Javascript
and Html Helpers.

The key part:

Keep in mind that scripts added from the layout, or elements in the layout will not be added to $scripts_for_layout.

So how do I do it then?

I guess I could add a <script src="/js/mega_drop.js"></script> to the default.ctp layout. That doesn't feel right though as it would tightly tie the layout and the element together.

Whats the CakePHP best practice way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小猫一只 2024-10-19 18:22:05

addScript() 不加载文件;它将实际代码添加到 $scripts_for_layout 变量中。这个想法是,布局是加载 JavaScript 文件和代码的一个很好的、常见的地方。这样,您就可以在一个位置输出所有代码 - 在头块中或在末尾 - 无论哪种方式都在一起。因此,如果您的视图中有 JavaScript 代码,而不是内联输出,您可以将其传递到布局。

加载脚本文件的最佳方法是使用 HTML Helper - echo $this->Html->script("script or array('of', 'scripts')");考虑到这一点,您可以在元素中 $this->set('scripts', 'mega_drop'); ,然后使用布局中的 $scripts 变量调用 Html Helper。

问题在于:如果从布局调用 nav_default.ctp,它将不起作用。 $this->set() 在视图内部工作(或从视图调用的元素)因为视图是在布局之前渲染的。如果您从布局中调用元素,那么设置 viewVars 以在布局中使用就为时已晚。 最好的办法是从控制器 set() 脚本变量并使用 if(isset($scripts)) { echo $this->Html->script($脚本); } 在布局中。

addScript() does not load a file; it adds actual code to the $scripts_for_layout variable. The idea being that the layout is a good, common place to load your JavaScript files and code. That way you can output all the code in one location - in the head block or at the end - either way it's together. So if you are in a situation where you've got JavaScript code in the view, rather than output it inline, you can pass it up to the layout.

The best way to load a script file is with the HTML Helper- echo $this->Html->script("script or array('of', 'scripts')"); With that in mind, you could $this->set('scripts', 'mega_drop'); in the element and then call the Html Helper with that $scripts variable from the layout.

The problem with that: it won't work if your nav_default.ctp is called from the layout. $this->set() works inside of a view (or an element called from a view) because the View is rendered before the Layout. If you are calling your element from the layout, then it is too late to be setting viewVars for use in the layout. The best thing to do is set() the scripts variable from the Controller and use a if(isset($scripts)) { echo $this->Html->script($scripts); } in the layout.

从来不烧饼 2024-10-19 18:22:05

正确且有效的 1.3.x CakePHP 2.0 Dev 来自 example.ctp 文件:

$this->addScript($this->Javascript->link('tab_enabler')); 
$this->addScript($this->Html->css('jquery.tabs'));

这是如何从视图中正确包含 CSS 和 JS 文件并添加变量 $scripts_for_layout< 的示例/code> 不会生成 W3C 验证错误,因为将链接添加到 中的 css 文件是不正确的

Correct and valid 1.3.x CakePHP 2.0 Dev is from example.ctp file:

$this->addScript($this->Javascript->link('tab_enabler')); 
$this->addScript($this->Html->css('jquery.tabs'));

This is an example of how to properly include CSS and JS files from the view and adding in the variable $scripts_for_layout to not generate validation error with the W3C as it is not correct to add the link to a css file in <BODY></BODY>

愿与i 2024-10-19 18:22:05

尝试

$this->Html->script('mega_drop', $inline=false);

在没有回声的情况下使用您的元素。

第二个参数表示将其添加到 $scripts_for_layout 变量中。

您应该能够在您的元素中执行此操作,以便仅在该元素包含时才包含 javascript。

try

$this->Html->script('mega_drop', $inline=false);

in your element without the echo.

The Second parameter says to add it to the $scripts_for_layout variable.

You should be able to do this in your element, so that the javascript is only included when the element is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文