CodeIgniter Forums
Where is the best place to load view for header. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Where is the best place to load view for header. (/showthread.php?tid=42716)



Where is the best place to load view for header. - El Forum - 06-16-2011

[eluser]mrwbsn[/eluser]
hello all.. sorry for this basic question, i want to know where the best place to load view header?! i usually load it in function construct,but when i dont need header i must make a new controller to solve this.any suggestion to make it more better.. thx.Big Grin


Where is the best place to load view for header. - El Forum - 06-17-2011

[eluser]CroNiX[/eluser]
I make a library that puts the header and footer automatically, which I autoload. Then in my controllers I just send my "content" view to the library which outputs the header, footer, login modules, widgets, etc., which are generated dynamically. There are several ways to achieve this. I want my controllers to ONLY deal with the functionality of those controllers.

Simple version of the library:

Code:
class Template
{
    
    function Template()
    {
        $this->CI =& get_instance();
    }

    function make_front($page_title, $page_h1, $meta_keywords, $meta_description, $content)
    {
        
        //assemble the header
        $head['page_title'] = $page_title;
        $head['page_h1'] = $page_h1;
        $head['meta_description'] = $meta_description;
        $head['meta_keywords'] = $meta_keywords;
        $header = $this->CI->load->view('frontend/header', $head, TRUE);
        
                //assemble footer
        $foot['states'] = $this->get_states();
        $footer = $this->CI->load->view('frontend/footer', $foot, TRUE);
        
        $out = $header . $content . $footer;
        $this->CI->output->set_output($out);
    }
}

Then in my controller, I just pass the view being generated by that controller to the template, which tacks on the header and footer:
Code:
$content = $this->load->view('my_content', $data, TRUE);
$this->template->make_front('page title', 'page H1', 'keywords', 'description', $content);



Where is the best place to load view for header. - El Forum - 06-17-2011

[eluser]CroNiX[/eluser]
And you could always set up parameters to pass along to the library which tell it to output the header, or not.


Where is the best place to load view for header. - El Forum - 06-17-2011

[eluser]mrwbsn[/eluser]
wohoo thx for the library mate. Big Grin i'll try this!!Smile