doutan8601 2019-07-31 08:28
浏览 63
已采纳

如何自动加载页眉和页脚视图?

I am new to using Codeigniter but I do have enough understanding on mvc structure and Codeigniter itself to do simple things like load views in the controller and auto load libraries e.c.t. But the issue im having is I have a header and footer view that I want to be loaded automatically every time I load a view file.

I have done some googling and a lot of suggestions are dated, or sometimes I simply dont understand the solution. I have made my header view and linked my css in it, and also created my footer view as well. So lets say I wanted to load the default welcome page like below:

public function index() {
      $this->load->view('welcome_message');
}

I can load them manually like so:

public function index() {
      $this->load->view('common/header');
      $this->load->view('welcome_message');
      $this->load->view('common/footer');
}

But what I would like is to just load the view like normal and have my header and footer loaded with it automatically, I understand this needs to be accomplished using a custom library with a template function of some sort, but again I dont know enough to just do that from scratch and was hoping perhaps someone could point me in the right direction.

  • 写回答

2条回答 默认 最新

  • dszm02606009 2019-07-31 08:50
    关注

    I did this and it works for me.

    MY_Loader.php

    class MY_Loader extends CI_Loader{
        public function template($content,$var=array()){
            $this->view('common/header');
            $this->view($content,$var);
            $this->view('common/footer');
        }
    }
    

    Put it in core folder.

    In your controller :

     public function index(){
       $content = "welcome_message";
       $data = array();
       $data['name'] = "Max";
       $data['country'] = "USA";
       $this->load->template($content,$data);
     }
    

    Call the data in view :

    <html>
      <?php echo $name.' - '.$country; ?>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部