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

#1
[eluser]mattbman[/eluser]
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?

Thanks

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

Code:
Controller:

Sample.php
class Sample extends Controller
{
   function Sample()
   {
     parent::Controller();

     // need this to use base_url() in the template.php
     $this->load->helper('url');
   }

   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);
   }
}


template.php
<html>
<head>
  <title><?php echo $title; ?></title>

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

<body>

   <div id="wrapper">
      <div id="header">
         &lt;?php // dynamic loading of header just specify the $header in the controller
          $this->load->view($header);
         ?&gt;
      </div>

      <div id="content">
         &lt;?php // dynamic loading of content just specify the $content in the controller
          $this->load->view($content);
         ?&gt;
      </div>

      <div id="footer">
         &lt;?php // dynamic loading of footer just specify the $footer in the controller
          $this->load->view($footer);
         ?&gt;
      </div>
   </div>

&lt;/body&gt;

&lt;/html&gt;


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

header.php
<h1>Welcome to the website</h1>

footer.php
<h1>Copyright 2008</h1>


Hope this helps!

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

#4
[eluser]crumpet[/eluser]
I use a template approach with a wrapper function for $this->load->view() which i put in MY_controller

Code:
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
Code:
$this->load->view('header');
$this->load->view($view);
$this->load->view('footer');

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

#5
[eluser]majidmx[/eluser]
Hey,
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,
MajiD

#6
[eluser]mattbman[/eluser]
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.

#7
[eluser]majidmx[/eluser]
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

#8
[eluser]mattbman[/eluser]
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.

#9
[eluser]crumpet[/eluser]
you could have two header files - one called logged_in, one called logged_out and then you can put logic in the controller.

#10
[eluser]mattbman[/eluser]
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.