Best method of including views within views in CodeIgniter

前端 未结 6 717
时光说笑
时光说笑 2020-11-28 02:57

I\'m starting a large codeigniter project and would like to try to create some reusable \'mini\' views for snippets of content like loops of data which may be displayed on d

6条回答
  •  误落风尘
    2020-11-28 03:32

    METHOD 1

    I use this method into my view to insert the include view where I want

    $this->load->view('include/include_view');
    


    METHOD 2

    or in the controller you can load more than a view like this:

    $this->load->view('header_view');
    $this->load->view('list_view');
    $this->load->view('footer_view');
    

    No one method is better than the other, it depends if you have to pass some data (in this case use method2) or if you want to include a view in a specific part of your main view (in this case is better to use method1)


    METHOD 3

    Passing data to your include view by your main view

    into your controller:

    $data['title'] = "Title";
    $this->load->view('main_view',$data);
    

    in your view

    $data2['title'] = $title;
    $this->load->view('include/include_view',$data2);
    

    If you want to pass entire data to your include view you can do in this way: in your controller:

    $data['nestedView']['title'] = 'title';
    

    in your view

    $this->load->view('includes/included_view', $nestedView);
    

提交回复
热议问题