Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices: What's the Best Way for Constructing Headers and Footers?

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.

like image 699
jmccartie Avatar asked Oct 07 '08 01:10

jmccartie


People also ask

Which view is best for adding headers and footers?

Add or change headers or footers in Page Layout view Click the worksheet where you want to add or change headers or footers. On the Insert tab, in the Text group, click Header & Footer. Excel displays the worksheet in Page Layout view.


1 Answers

Actually, after researching this quite a bit myself, I came to the conclusion that the best practice for including headers and footers in MVC is a third option - namely extending a base controller. That will give you a little more flexibility than the text's suggestion, particularly if you're building a very modular layout (not just header and footer, also sidebar panels, non-static menus, etc.).

First, define a Base_controller class, in which you create methods that append your page elements (header, footer, etc.) to an output string:

class Base_controller extends Controller
{
    var $_output = '';

    function _standard_header($data=null)
    {
        if (empty($data))
            $data = ...; // set default data for standard header here

        $this->_output .= $this->load->view('header', $data, true);
    }

    function _admin_header($data=null)
    {
        if (empty($data))
            $data = ...; // set default data for expanded header here

        $this->_output .= $this->load->view('admin_header', $data, true);
    }

    function _standard_page($data)
    {
        $this->_standard_header();
        $this->_output .=
            $this->load->view('standard_content', $data, true);
        echo $this->_output; // note: place the echo statement in a
                             // separate function for added flexibility
    }

    function _page_with_admin_header($data)
    {
        $this->_admin_header($data);
        $this->_output .=
            $this->load->view('standard_content', $data, true);
        echo $this->_output;
    }
}

Then, in your page controllers, simply extend the base class and call your functions to build the page.

class Page_controller extends Base_controller
{
    function index()
    {
        $data = ...; // Set content data here
        $this->_standard_page($data);
    }

    function admin()
    {
        $data = ...; // Set content and header data here
        $this->_page_with_admin_header($data);
    }
}

Using a base controller, you can achieve very clean code in your individual page controllers AND have separate views for elements on the page (allowing code reuse in both views and controllers). All you need to do is define your common page 'sections' (what you might be tempted to call 'fragments') as functions in your base controller.

And if the base controller should start to grow uncontrollably (which can happen on large sites), you can rearrange some of its less-general functions by placing them in subclasses and letting the corresponding page controllers extend those instead of the original base controller.

Enjoy!

like image 170
Jens Roland Avatar answered Sep 27 '22 21:09

Jens Roland