CodeIgniter Forums
Problem with views, any good way to automatically load header, footer etc? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem with views, any good way to automatically load header, footer etc? (/showthread.php?tid=14675)



Problem with views, any good way to automatically load header, footer etc? - El Forum - 01-11-2009

[eluser]Vitsaus[/eluser]
I'm running into a small code repetation problem here. I don't want to paste this into every controller:

Code:
$this->load->view('header');
$this->load->view('content',$data['content']);
$this->load->view('navigation',$data['navigation']);
$this->load->view('footer');

Is there any nice way to automatically load, lets say header and footer-views, since they are obviously parts that I don't need to change dynamically anyway?


Problem with views, any good way to automatically load header, footer etc? - El Forum - 01-11-2009

[eluser]darkhouse[/eluser]
There are a couple solutions. You could use a template system, Colin Williams wrote one that seems to be pretty popular, though I have not tried it. [url="http://williamsconcepts.com/ci/codeigniter/libraries/template/"]http://williamsconcepts.com/ci/codeigniter/libraries/template/[/url]

Or, you could do what I do and just setup your controller like this:

Code:
$data['content'] = $content_array;
$data['navigation'] = $navigation_array;

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

And then setup your view like this:

Code:
<?php $this->load->view('header'); ?>

some html here

<?php print_r($content); ?>

more html

<?php $this->load->view('navigation', $navigation);

final html, or whatever your structure is

<?php $this->load->view('footer'); ?>

That's generally what I do, just do all the common view loading from within the main view, so I only have one view being loaded in my controller. But check out that template library, it might be a better solution now and for future projects.