doutan8601 2019-07-31 16: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 16: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条)

报告相同问题?

悬赏问题

  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题