我们在做web编程的过程中,为了减少一些公共的部分重复引用和代码过多的冗余,我们通常会使用include的方式来进行引入,在HTML中也可以实现相应的功能,有兴趣可以查看: 在html文件中引入另一个html文件 .
本文主要介绍使用CI框架实现头部和底模板的功能:
views->include
---------footer.php
---------header.php
我们首页需要创建一个template.php文件,里面写上如下代码:
$this->load->view('include/header');
$this->load->view($content_text);
$this->load->view('include/footer');
在Controller里面用变量把View名称存储起来,然后调用。
public function index()
{
$data['content_text'] = 'home';
$this->load->view('template',$data);
}
public function add()
{
$data['content_text'] = 'addcategory';
$this->load->view('template',$data);
}