嗨,只需将您的侧边栏创建为视图并将其传递给您的布局视图,我已经创建了一个简单的布局系统,看看它
首先在 application/core 目录下创建 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>