Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Codeigniter模板库,添加_js()方法_Codeigniter_Templates - Fatal编程技术网

Codeigniter模板库,添加_js()方法

Codeigniter模板库,添加_js()方法,codeigniter,templates,Codeigniter,Templates,使用 当我尝试使用add_js()函数时,它会出错。我将regions$\u scripts添加到模板头文件中,但加载页面时会显示未定义变量\u scripts 有什么想法吗 将更改为 很明显,我丢失了错误,但是js文件仍然没有加载 我通过add_js()方法在每一步都做了一次回音,结果是正确的。我还输出了$this->js,它们都有正确的输出。因此,它通过get_js方法fine设置类变量fine,但我没有在实际页面中添加任何内容 对不起,现在换台电脑了 以下是控制器方法: function

使用

当我尝试使用
add_js()
函数时,它会出错。我将regions
$\u scripts
添加到模板头文件中,但加载页面时会显示未定义变量
\u scripts

有什么想法吗

更改为

很明显,我丢失了错误,但是js文件仍然没有加载


我通过
add_js()
方法在每一步都做了一次回音,结果是正确的。我还输出了
$this->js
,它们都有正确的输出。因此,它通过
get_js
方法fine设置类变量fine,但我没有在实际页面中添加任何内容

对不起,现在换台电脑了

以下是控制器方法:

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>

我不确定这是否重要,但为什么不在“单一”模板中定义页眉和页脚区域呢?我锁定此库的原因之一是,我不必为每个控制器方法添加页眉和页脚视图。你能发布你的模板库配置吗?事实上我从来没有想到过,但我现在会这么做。我想这只是旧习惯吧。模板库在CI自动加载配置中自动加载。请记住,上述代码尚未根据您的建议进行更改。谢谢你。我在上面添加了模板配置文件。这与php和短标记有关。
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;
   }
/*
|--------------------------------------------------------------------------
| 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 */