我在Codeigniter中找到了初学者模板的这个很棒的链接:
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 :(得分:0)
你好只是创建你的侧边栏作为视图并在你的布局视图中传递它我已经创建了一个简单的布局系统看看它
首先在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>