Welcome Guest, Not a member yet? Register   Sign In
Where to include header and footer?
#1

[eluser]Arun Joshi[/eluser]
I have created two files (header.php and footer.php) in application/view folder. I want to include these two files in all pages in my application. So where to include this header and footer pages? Is there any CI technique for this?
#2

[eluser]Dam1an[/eluser]
There are a number of common techniques for this

Loading the header and footer manually in each controller function
Code:
function index() {
    $this->load->view('header');
    $this->load->view('content');
    $this->load->view('footer');
}

Calling the header and footer from within the view
Code:
// view file
$this->load->view('header');
// my view content goes here
$this->load->view('footer');

2 levels of views (This is my current method)
Code:
function index() {
    $this->load->view('master', array('content'=>'content_view'));
}

// master view
$this->load->view('header');
$this->load->view($content_view);
$this->load->view('footer');

You will obviously want to also pass data arrays to the views
#3

[eluser]Arun Joshi[/eluser]
Hi Dam1an,

if I am going with third option, Do I need to include that code in all controllers?
#4

[eluser]Dam1an[/eluser]
No, with the 3rd option, all you need is the first line, the rest was just showing what would be in the master view file
#5

[eluser]Arun Joshi[/eluser]
can u explain?
Code:
$this->load->view($content_view);
How $content_view got data?

Sorry, I am a just beginner in CI
#6

[eluser]Dam1an[/eluser]
Yeah, if you want to pass data into the $content_view view, you need to pass it as ann array, the problem is, you passed it in as an array into the master view. The way I get around this is to pass the data array in as an array, so its a normal array by the time you pass it into the main content view

I've actually moved all of that into a seperate library in my application now, but to keep it simple, if you have a MY_Controller (your own controller which extends the CI one) and do all the common data initialisation in there, and then stick your data array (which has stuff specific to the unique part of the view) into the master data array (which you setup in the constructor, and has everything for the header and footer)

Does that make sense?
#7

[eluser]Arun Joshi[/eluser]
Thanks.

let me to check it,




Theme © iAndrew 2016 - Forum software by © MyBB