2010-06-15 94 views
0

使用This Template LibraryCodeigniter模板庫,add_js()方法

當我嘗試使用add_js()函數時,它會出錯。我將區域$_scripts添加到模板頭文件中,但是當我加載頁面時,它表示未定義的變量_scripts

任何想法?

改變了<?= $_scripts ?> to <?= (isset($_scripts)) ? $_scripts : 「」; ?>

,顯然我輸了錯誤,但js文件仍然沒有加載。

雖然add_js()方法和輸出正確,但我在每一步都做了迴音。我也做了$this->js的輸出,他們都有正確的輸出。所以它通過get_js方法很好,並設置類變量很好,但我沒有得到任何添加到實際頁面。

回答

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 */ 
+0

我不確定它是否重要,但爲什麼不在「單個」模板中定義頁眉和頁腳區域?我鎖定到這個庫的原因之一是,我不必爲每個控制器方法添加頁眉和頁腳視圖。 你可以發佈你的模板庫配置嗎? – someoneinomaha 2010-06-15 20:38:04

+0

其實它從來沒有發生過,但我現在會這樣做。我猜只是老習慣。 模板庫在CI自動載入配置中自動加載。 請記住,上述代碼並未隨您的建議而改變。感謝那。 我已經添加了上面的模板配置文件。 – Karl 2010-06-15 20:41:26

+0

這與php和短標籤有關。 – Karl 2010-06-15 23:12:47