Best Practices: What's the Best Way for Constructing Headers and Footers?
codeigniter, php, templates
Solution
You could also try it this way -- define a default view template, which then pulls in the content based on a variable ('content' in my example) passed by the controller.
In your controller:
$data['content'] = 'your_controller/index';
// more code...
$this->load->vars($data);
$this->load->view('layouts/default');
Then define a default layout for all pages e.g. views/layouts/default.php
// doctype, header html etc.
<div id="content">
<?= $this->load->view($content) ?>
</div>
// footer html etc.
Then your views can just contain the pure content e.g. views/your_controller/index.php might contain just the variables passed from the controller/data array
<?= $archives_table ?>
<?= $pagination ?>
// etc.
More details on the CI wiki/FAQ -- (Q. How do I embed views within views? Nested templates?...)
Problem
What's the best way for constructing headers, and footers? Should you call it all from the controller, or include it from the view file? I'm using CodeIgniter, and I'm wanting to know what's the best practice for this. Loading all the included view files from the controller, like this? ``` class Page extends Controller { function index() { $data['page_title'] = 'Your title'; $this->load->view('header'); $this->load->view('menu'); $this->load->view('content', $data); $this->load->view('footer'); } } ``` or calling the single view file, and calling the header and footer views from there: ``` //controller file class Page extends Controller { function index() { $data['page_title'] = 'Your title'; $this->load->view('content', $data); } } //view file <?php $this->load->view('header'); ?> <p>The data from the controller</p> <?php $this->load->view('footer'); ?> ``` I've seen it done both ways, but want to choose now before I go too far down a path.