0

I'm wondering how i can open a view in a view with CodeIgniter.

I think about this: In each controller, i will call the default index.php-view. There i will add a placeholder to open a view for this controller.

Is this an idea? Or is it best practice create different views for each controller, and include the view with: $this->view('header');?

I'm new with CI, FYI ;-)

Hkachhia
  • 4,463
  • 6
  • 41
  • 76
  • possible duplicate of [Best method of including views within views in CodeIgniter](http://stackoverflow.com/questions/15221371/best-method-of-including-views-within-views-in-codeigniter) – Dennis Rasmussen Dec 08 '14 at 14:37

1 Answers1

1

this is the easiest most flexible method i've found so far. create a template file in your views folder that calls the commonly used elements like header, nav bar, footer etc.

the template has open variables for passing specific content, for example lets make the template: views/template_web.php and then include some files to create the basic page structure views/tmpl_web_head.php views/tmpl_web_close.php views/tmpl_web_foot.php

now create a folder called 'web' for the specific content views/web/

with some specific files for web views/web/tmpl_web_head.php views/web/navbar.php views/web/navbar_bottom.php

so here is the template file views/template_web.php

// generic header 
$this->load->view('tmpl_web_head'); 

// folder with views specific for "web"
$templatefolder = 'web/';

// header and navbar specific to web   
$this->load->view($templatefolder.'header_top'); 

$this->load->view($templatefolder.'navbar'); 

// content that is passed from the controller    
if(isset($content01))
$this->load->view($templatefolder.$content01);

if(isset($content02))
$this->load->view($templatefolder.$content02);

if(isset($content03))
$this->load->view($templatefolder.$content03);

if(isset($content04))
$this->load->view($templatefolder.$content04);

if(isset($content05))
$this->load->view($templatefolder.$content05);

if(isset($content06))
$this->load->view($templatefolder.$content06);

$this->load->view($templatefolder.'navbar_bottom');


$this->load->view('tmpl_web_close');     
$this->load->view('tmpl_web_foot'); 

once this template is set up its very easy to pass content

in your controller

  function doSomethingWonderful(){

  // whatever is happening in the controller 
  $data['results'] = $this->model->doSomething ;

   // the views you want shown on the page
   $data['content01'] = 'results_view';
   $data['content02'] = 'search_form';
   $data['content03'] = 'latest_news';

   // just one view call, the content is passed automatically 
   $this->load->view( 'template_web', $data );


   }

there is a lot great about this but it really shines when you are making different versions of a site. you can just create a different template like say a web_beta and put those files in a web_beta folder

then in your controller everything stays the same except you call the beta template in the view call.

 $this->load->view( 'template_web_beta', $data );

this allows for very quickly updating a website, making changes, etc.

cartalot
  • 3,147
  • 1
  • 16
  • 14