当我尝试使用add_js()
函数时,它会出错。我将区域$_scripts
添加到模板头文件中,但是当我加载页面时,它会显示未定义的变量_scripts
。
有什么想法吗?
更改了<?= $_scripts ?> to <?= (isset($_scripts)) ? $_scripts : “”; ?>
显然我输了错误但是js文件仍然没有加载。
我通过add_js()
方法在每一步都做了回音,输出是正确的。我也输出了$this->js
,他们都有正确的输出。因此它可以很好地通过get_js
方法并将类变量设置为正常但我没有将任何内容添加到实际页面中。
答案 0 :(得分:0)
抱歉不同的电脑吧。
这是控制器方法:
function registrationForm(){
$this->template->set_template('single');
$this->template->write_view('header', 'templates/header_template');
$this->template->write_view('footer', 'templates/footer_template');
$this->template->write_view('center', 'user/registration_form');
$this->template->add_js('js/jquery.min.js');
$this->template->add_js('js/validate.jquery.js');
$this->template->render();
}
这是头部:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="<?= base_url() ?>css/style.css" type="text/css" rel="stylesheet" />
<?= (isset($_scripts)) ? $_scripts : ""; ?>
<?= (isset($_styles)) ? $_styles : ""; ?>
</head>
和add_js()方法:
function add_js($script, $type = 'import', $defer = FALSE)
{
$success = TRUE;
$js = NULL;
$this->CI->load->helper('url');
switch ($type)
{
case 'import':
$filepath = base_url() . $script;
$js = '<script type="text/javascript" src="'. $filepath .'"';
if ($defer)
{
$js .= ' defer="defer"';
}
$js .= "></script>";
break;
case 'embed':
$js = '<script type="text/javascript"';
if ($defer)
{
$js .= ' defer="defer"';
}
$js .= ">";
$js .= $script;
$js .= '</script>';
break;
default:
$success = FALSE;
break;
}
// Add to js array if it doesn't already exist
if ($js != NULL && !in_array($js, $this->js))
{
$this->js[] = $js;
$this->write('_scripts', $js);
}
return $success;
}
template.php配置文件
/*
|--------------------------------------------------------------------------
| Default Template Configuration (adjust this or create your own)
|--------------------------------------------------------------------------
*/
$template['default']['template'] = 'templates/default_template';
$template['default']['regions'] = array(
'header',
'left',
'right',
'footer',
);
$template['default']['parser'] = 'parser';
$template['default']['parser_method'] = 'parse';
$template['default']['parse_template'] = FALSE;
/*
|--------------------------------------------------------------------------
| Default Template Configuration (adjust this or create your own)
|--------------------------------------------------------------------------
*/
$template['single']['template'] = 'templates/single_template';
$template['single']['regions'] = array(
'header',
'center',
'footer',
);
$template['single']['parser'] = 'parser';
$template['single']['parser_method'] = 'parse';
$template['single']['parse_template'] = FALSE;
/* End of file template.php */
/* Location: ./system/application/config/template.php */