kilrizzy
kilrizzy

Reputation: 2943

Getting the hang of CodeIgniter - Templating / loading views

Attempting to learn CI and going through the docs to get a better understanding. Without getting a separate library, I could make a template by including a list of views like so:

$this->load->view('header');
$this->load->view('navigation');
$this->load->view('sidenav_open');
$this->load->view('blocks/userinfo');
$this->load->view('blocks/stats');
$this->load->view('sidenav_close');
$this->load->view('content',$data);
$this->load->view('footer');

This makes sense but would I actually have that on each of my controllers (pages)? Not sure if there is a way to include this in the initial controller (welcome) and then in the others somehow reference it? Or perhaps there is something I am missing completely

Upvotes: 1

Views: 1933

Answers (2)

jedd
jedd

Reputation: 56

Loading views from within views can lead to confusion.

Extending the Controller class hides much of the complexity that comes from that approach, but still utilises the idea of generating common views (footer, header, navigation bars, etc) by rendering them once on every page load.

Specifically, consult the CI User Guide and wiki for references to MY_Controller - you extend this by creating a MY_Controller.php file in the ./libraries directory.

In there you can call view fragments, also utilising the third-parameter=true feature of the load->view() call. You load these into $this->data - for example loading the footer into $this->data['footer']. In your various controllers, continue adding view data to $this->data. In your views - I typically use a template that does little other than skeleton HTML and some basic CSS, and then echos the entire header, footer, nav and main content lumps as variables taken from $this->data

Added bonus - if you're new to CI, you'll likely soon be looking for how to do other things that a MY_Controller will make easy for you :)

I've got a wiki page on simplifying the generation and display of view partials, as you're trying to do here, using MY_Controller at:

https://github.com/EllisLab/CodeIgniter/wiki/Header-and-Footer-and-Menu-on-every-page---jedd

Upvotes: 1

kevtrout
kevtrout

Reputation: 4984

You can load views from within a view file. for example, consider a generic page template called page_template.php:

<html>
<body>
  <div id = "header">
      <?php $this->load->view('header');?>
      <?php $this->load->veiw('navigation');?>
  </div>
  <div id = "sidenav">
     <?php $this->load->view('sidenav');?>
  </div>
  <div id = "content">
     <?php echo $content;?>
   </div>

  <div id = "footer">
      <?php $this->load->view('footer');?>
 </body>
 </html>

Load your more dynamic areas by making use of codeigniter's abiltiy to return a view as a variable in your controller:

$template['content'] = $this->load->view('content',$data,TRUE);
$this->load->view('page_template',$template);

By passing TRUE to the load function, CI will return the data from the view rather than output to the screen.

Your sidenav section could be it's own view file, sidenav.php, where you have your 'blocks' hard-coded or loaded similar to the above example.

I've done it both ways, including every stinking bit of views in each controller method, and by using a page template that loads sub-views and dynamic areas, and by far, the second method makes me happier.

Upvotes: 9

Related Questions