有人可以解释一下 CakePHP layout.ctp 文件中使用的这两行吗?似乎这两行都用于添加 css。那有什么区别呢?它们是如何工作的。
$this->Html->css('cake.generic')
$this->fetch('css')
最佳答案
请参阅有关为 script and CSS files 使用 block 的文档.
在您的 View 中,您可以使用:
$this->Html->css('cake.generic')
在您的布局中,您可以使用:
$this->fetch('css')
A layout contains presentation code that wraps around a view. Anything you want to see in all of your views should be placed in a layout.
这意味着在定义布局时,使用 $this->fetch('css')
将添加 View 中使用的任何 css
block ,因此如果在你看来你会把两者都放在:
$this->Html->css('cake.generic')
$this->Html->css('cake.special')
在您的布局中使用 $this->fetch('css')
将包括两者。请注意,如果不先定义 block 内容,它不会调用任何东西。在 block 调用之前在布局文件中添加 $this->Html->css('cake.generic')
确保它在尚未包含在定义的 View 中时被添加。
关于css - CakePHP : Difference between "$this->fetch(' css') & $this->Html->css ('cake.generic' ) in cake php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20676976/