2013-07-10 136 views
0

我發現笨的初學者模板這個偉大的鏈接:笨模板庫編輯

http://net.tutsplus.com/tutorials/php/an-introduction-to-views-templating-in-codeigniter/

真的,這是我第一次已經能夠遵循這個問題一步的教程並且有點理解邏輯。

但是,雖然它允許我指定一個模板(例如默認),但它限制了我必須在該內容視圖文件中包含所有HTML正文代碼。我想將它分解爲標題,主要內容,可選的側邊欄和頁腳。我的一些網頁是三列(有左側欄和右側欄),有些是兩欄(有相同的右側欄),有些是單欄(沒有側欄),因此,我的想法是有3個相應的模板 - 但是我必須在許多不同的視圖文件中重複右側欄的代碼,這將使編輯更加困難 - 並最終失去了製作模板的目的系統。

是否有可能通過編輯現有代碼來實現我想要的功能,還是有人可以向我推薦的另一個佈局/模板庫?

PS。我曾考慮過這樣一個事實,即我可以在庫文件中編輯以下行以實現加載的頁眉和頁腳,但是主體代碼和可選的側欄是我的主要障礙,因爲我無法在此處加載側邊欄而無需應用所有模板 - 包括一列模板。

$this->ci->load->view('header_view, $data); 
    $this->ci->load->view('templates/'.$tpl_view, $data); 
    $this->ci->load->view('footer_view, $data); 

回答

0

喜剛創建側邊欄的視圖,並通過它在你這裏的佈局圖,我有創建一個簡單的佈局系統必須看看它

首先在應用程序中創建MY_Controller /核心目錄複製下面的代碼

class MY_Controller extends CI_Controller{ 

//all you default views 
public $layout = 'default'; 
public $header = 'default_heder'; 
public $right_sidebar = 'default_right_sidebar'; 
public $left_sidebar = 'default_left_sidebar'; 
public $footer = 'default_footer'; 

public function __construct() { 
     parent::__construct(); 
} 

public build($view,$data = array()){ 

     $layout_data = array(); 
     $layout_data['header'] = $this->load->view($this->header,$data,TRUE); 
     $layout_data['right_sidebar'] = $this->load->view($this->right_sidebar,$data,TRUE); 
     $layout_data['left_sidebar'] = $this->load->view($this->left_sidebar,$data,TRUE); 
     $layout_data['footer'] = $this->load->view($this->footer,$data,TRUE); 
     $layout_data['body_content'] = $this->load->view($view,$data,TRUE); 

     $this->load->view($this->layout,$layout_data); 

} 

} 

第二創建任何控制器和帶有MY_Controller我已創建示例頁面控制器

class Page extends MY_Controller{ 

public function __construct() { 
     parent::__construct(); 
} 

public index(){ 
      $data['content'] = 'content here'; 
      $this->build('page_view',$data); 
} 

public one_coulmn(){ 
      $this->layout = 'one_column'; // change layout view 
      $this->right_sidebar = 'inner_right_sidebar'; // change sidebar view 
      $data['content'] = 'content here'; 
      $this->build('page_view',$data); 
} 

} 
擴展它

這裏是你的3列視圖的例子

// three col layout 
<div id="header"><? echo $header ?></div> 
<div id="left_sidebar"><? echo $left_sidebar ?></div> 
<div id="body_content"><? echo $body_content?></div> 
<div id="right_sidebar"><? echo $right_sidebar ?></div> 
<div id="footer"><? echo $footer ?></div>