Welcome Guest, Not a member yet? Register   Sign In
Problem with views, any good way to automatically load header, footer etc?
#1

[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?
#2

[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.




Theme © iAndrew 2016 - Forum software by © MyBB