Vic*_*tor 3 cakephp cakephp-3.0
我正在使用CakePHP 3.3.10.我需要将JavaScript文件添加到特定视图.
// default.ctp
// I need to remove this script in default.ctp and load only in a specific view.
<?= $this->Html->script(['otherfile.js', 'otherfile.js', folder/scripts.js']) ?>
Run Code Online (Sandbox Code Playgroud)
如何仅在此视图中加载js文件?:
// index.ctp
// I need to load the scripts.js file only in this view
Run Code Online (Sandbox Code Playgroud)
小智 7
<?php echo $this->Html->css('style.css',['block'=>true]); ?>
Run Code Online (Sandbox Code Playgroud)
block = true选项使css加载到head标签内。它将加载到您将代码<?php echo $ this-> fetch ('css'); ?>
放在模板中的位置
小智 5
转到index.ctp文件并在底部插入此代码.
对于JS
echo $this->Html->script('/otherdir/scripts');
Run Code Online (Sandbox Code Playgroud)
要么
echo $this->Html->script('http://code.jquery.com/jquery.min.js');
Run Code Online (Sandbox Code Playgroud)
将输出:
<script src="http://code.jquery.com/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
第一个参数可以是包含多个文件的数组.
echo $this->Html->script(['jquery', 'wysiwyg', 'scripts']);
Run Code Online (Sandbox Code Playgroud)
将输出:
<script src="/js/jquery.js"></script>
<script src="/js/wysiwyg.js"></script>
<script src="/js/scripts.js"></script>
Run Code Online (Sandbox Code Playgroud)
您可以使用块选项将脚本标记附加到特定块:
echo $this->Html->script('wysiwyg', ['block' => 'scriptBottom']);
Run Code Online (Sandbox Code Playgroud)
然后,在您的布局中,确保具有所述块以便输出:
<?= $this->fetch('scriptBottom')?>
Run Code Online (Sandbox Code Playgroud)
对于CSS
这种CSS包含方法假定如果路径不以'/'开头,则指定的CSS文件驻留在webroot/css目录中.
echo $this->Html->css('forms');
Run Code Online (Sandbox Code Playgroud)
将输出:
<link rel="stylesheet" href="/css/forms.css" />
Run Code Online (Sandbox Code Playgroud)
第一个参数可以是包含多个文件的数组.
echo $this->Html->css(['forms', 'tables', 'menu']);
Run Code Online (Sandbox Code Playgroud)
将输出:
<link rel="stylesheet" href="/css/forms.css" />
<link rel="stylesheet" href="/css/tables.css" />
<link rel="stylesheet" href="/css/menu.css" />
Run Code Online (Sandbox Code Playgroud)