• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best Strategy for Dynamic Header and Footer sections?

This is a question for those that have worked with the framework for a while, just asking on opinions regarding the best practice to accomplish what I need.

I have a block of code that I want as a header and footer on all of my pages (multiple controllers), and I am struggling as to what the best way to go about putting that in the framework. Here is what I've come up with so far:

1. Hooks using pre_controller and post_controller - this seems to work well if it is a simple block of HTML text, however my header also needs to get access to the session information to display whether a person is logged in or not. The class for the hook doesn't seem to like $CI =& get_instance();, so I have trouble loading any libraries within that hook class

2. Extend the controller to MY_controller and include my header and footer functions there. - I don't mind this as a solution, but I would need to add 2 lines of code to every page function. Is there a way to make this call the site header and footer automatically?

3. Create a model that will output the header and footer. - I am still in the same situation as #2 on this one, that I would have to call the functions for that model on every output function so that it will display.

What has worked for you guys? Is there some idea that I am missing?


[eluser]Rey Philip Regis[/eluser]
Well....I think what you need is to create a master template and in that master template you specify the parts of your page including the header, content and the footer. Ill show you a sample on how to do it. This is what we call loading a view within a view.


class Sample extends Controller
   function Sample()

     // need this to use base_url() in the template.php

   function index()
      $data['title'] = 'Sample Template';
      $data['header'] = 'header_page'; // name of the view
      $data['content'] = 'main_page'; // name of the view
      $data['footer'] = 'footer_page'; // name of the view

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

  <title><?php echo $title; ?></title>

  <link type="text/css" rel="stylesheet" href="<?php echo base_url().'system/css/default.css'; ?>" />


   <div id="wrapper">
      <div id="header">
         &lt;?php // dynamic loading of header just specify the $header in the controller

      <div id="content">
         &lt;?php // dynamic loading of content just specify the $content in the controller

      <div id="footer">
         &lt;?php // dynamic loading of footer just specify the $footer in the controller



<h1>This is the dynamic content (main page) of the page.....</h1>

<h1>Welcome to the website</h1>

<h1>Copyright 2008</h1>

Hope this helps!

[eluser]Michael Wales[/eluser]
I just create controllers for the header and footer, then use Wick to make the magic happen.

I use a template approach with a wrapper function for $this->load->view() which i put in MY_controller

function _run($view, $title = 'Default Title')
   $this->template['title'] = $title;
   $this->temp;ate['view'] = $view
   //any other code you need to deal with your layout
   $this->load->view('loader', $this->template);

b/c it extends controller $this->template is available from all your controller functions so you can just add data that way. Then you make a loader.php view like this

So now in your controller functions instead of $this->load->view('main');
You go $this->_run('main');
No extra lines of code

Loading header and footer in the the desired view is the best way as Rey Philip mentioned.
Keep the code as simple as possible.

Take care,

OK, I have implemented a main templating structure as recommended by Rey Philip, however here would be my next question:
Where do I put the dynamic PHP coding that is required for the header? Do I put that in the header?

Again, if I put it in the controller, I would have to duplicate it for every controller.

Well, it depends on what kind of dynamic information you have.
If it's independent from the Controller, you can put it in Header file.
If not, you may pass the necessary info as a variable to main view then pass it again to header.
Or you can just right a new library/Plugin and call it directly from the header file to get the desired info.

Let me know how did you get the best result
Take care,
MajiD Fatemian

Right now, the dynamic information determines whether the person is logged in or not (checks a session variable) and changes the top menu based on that information.

For now, I just put the coding in the header file, I may need to do something different if there is more information I need to process with it.

you could have two header files - one called logged_in, one called logged_out and then you can put logic in the controller.

Except that logic would have to duplicated in every controller, that is what I am trying to avoid.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.