这是我通常对任何 CodeIgniter 项目所做的事情......
我扩展了 HTML 帮助程序 ( application/helpers/MY_html_helper.php
) 以包含一个js()
函数:
function js($js)
{
$js_base_path = '';
// If you have multiple JS files, pass an array
if(is_array($js))
{
foreach($js as $script_src)
{
if(strpos($script_src, 'http://') === false && strpos($script_src, 'https://') === false)
{
$js_base_path = base_url() . 'js/';
}
echo '<script src="' . $js_base_path . $script_src . '"></script>';
}
}
// Otherwise, a string will do
else
{
if(strpos($js, 'http://') === false && strpos($js, 'https://') === false)
{
$js_base_path = base_url() . 'js/';
}
echo '<script src="' . $js_base_path . $js . '"></script>';
}
}
在我的控制器中,我将在页脚(在您的情况下为页眉)包含一个js
参数:
$this->load->view('_footer', array('js'=>'jquery.cycle.lite.js'));
我的_footer
视图运行这样的js()
功能:
<?php
if(isset($js))
{
$this->load->helper('html');
js($js);
}
?>
这对我来说非常有用,请随时根据您的需要对其进行自定义。