CodeIgniter Forums
Header & footer to all of the pages - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Header & footer to all of the pages (/showthread.php?tid=26559)



Header & footer to all of the pages - El Forum - 01-17-2010

[eluser]Fr3aked0ut[/eluser]
Hi. I am using couple of methods (e.g post, index, page) and for each one I've got to write the same codes to add the header & the footer.
I want to know how I can write the codes only once and it will show up in all of the methods.

Thanks in advance.


Header & footer to all of the pages - El Forum - 01-17-2010

[eluser]fraserk[/eluser]
You can use a one of the many template systems.
Take a look at this. http://codeigniter.com/wiki/Category:Approaches::Header_and_Footer_and_Menu_on_every_page/


Header & footer to all of the pages - El Forum - 01-17-2010

[eluser]jedd[/eluser]
[quote author="Fr3aked0ut" date="1263754007"]
I am using couple of methods (e.g post, index, page) and for each one I've got to write the same codes to add the header & the footer.
[/quote]

fraserk is on the money here.

If you read the [url="/wiki/FAQ"]FAQ[/url] you'll find this question is asked and answered there, with pointers to a few other resources through-out the wiki. The forums - if you search for 'header footer', will in turn reveal very many threads on this already - many (in the past 12 months) contain a response by me that will read very much like this one.


Header & footer to all of the pages - El Forum - 01-17-2010

[eluser]Fr3aked0ut[/eluser]
OK so im using Chuck Son's take:
http://codeigniter.com/wiki/Chuck_Son's_take/
Now, this is my hook (config):
Code:
$hook['pre_controller'] = array(
                      'class'  => 'Template',
                      'function' => 'header',
                      'filename' => 'template.php',
                      'filepath' => 'hooks'
                      );
$hook['post_controller'] = array(
                      'class'  => 'Template',
                      'function' => 'footer',
                      'filename' => 'template.php',
                      'filepath' => 'hooks'
                      );

And this is template.php:
Code:
<?php

class Template extends Controller {
      function Template() {
        parent::Controller();
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->database();
      }
        
    function header()
    {
//code...
        $this->load->view('header');
    }
      function footer() {
          $this->load->view('footer');
      }
}

?>
When I get into my site, this error is displayed:
Quote:Fatal error: Call to a member function _assign_libraries() on a non-object in /system/libraries/Loader.php on line 1059



Header & footer to all of the pages - El Forum - 01-17-2010

[eluser]jedd[/eluser]
[quote author="Fr3aked0ut" date="1263773578"]OK so im using Chuck Son's take:
http://codeigniter.com/wiki/Chuck_Son's_take/
[/quote]

Wow - I hadn't even noticed ChuckSon had added a take.

Having a very quick read through it now, my initial impression is that this approach blows goats.

I'd suggest you read mine first, even if you later decide to go with hooks rather than MY_Controller it will give you some good insights.

Plus mine uses more colours.


Header & footer to all of the pages - El Forum - 01-17-2010

[eluser]Fr3aked0ut[/eluser]
Yours is too complicated.. Chuck Son's is couple of rows Big Grin


Header & footer to all of the pages - El Forum - 01-17-2010

[eluser]Dyllon[/eluser]
[quote author="Fr3aked0ut" date="1263777106"]Yours is too complicated.. Chuck Son's is couple of rows Big Grin[/quote]

Extending the controller is simple and offers a lot of flexibility beyond view partials, it's also a very common practice.
You may as well learn now and start doing it or you'll be kicking yourself when you realize it was the right approach in the near future.


Header & footer to all of the pages - El Forum - 01-18-2010

[eluser]Fr3aked0ut[/eluser]
I've made it! It is awesome! jedd, well done, thank you so much.