CodeIgniter Forums
What's the practical way to load header and footer - 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: What's the practical way to load header and footer (/showthread.php?tid=9007)

Pages: 1 2


What's the practical way to load header and footer - El Forum - 06-08-2008

[eluser]EEssam[/eluser]
Hello,

I'm wondering what is the most practical way to load a header and a footer.

I know I can do:

$this->load->view('header');
$this->load->view('myview');
$this->load->view('footer');

But I just don't see how I should call this in almost each function in my controller classes.

Please advise.


What's the practical way to load header and footer - El Forum - 06-08-2008

[eluser]Gavin Blair[/eluser]
I start every view with
<?php include 'header.php'; ?>

and end each view with
<?php include 'footer.php; ?>

and everything else goes in between.


What's the practical way to load header and footer - El Forum - 06-08-2008

[eluser]EEssam[/eluser]
I found this method used by scaffolding:

<? $this->load->view('header') ?>

Not sure if it's the best.


What's the practical way to load header and footer - El Forum - 06-08-2008

[eluser]Armchair Samurai[/eluser]
The basic one I use for simple sites is to have a template view file which is set up something like this:
Code:
<?php
/* The view file: template.php */

$this->load->view('includes/'.empty($header) ? 'header' : $header);
$this->load->view($content);
$this->load->view('includes/'.empty($footer) ? 'footer' : $footer);

?>
Then just call the content from the controller:
Code:
/* In your a controller function */

$this->load->vars(array(
    'content' => 'myview',
    'var' => 'foo',
    'other_var' => 'bar',
));
$this->load->view('template');



What's the practical way to load header and footer - El Forum - 06-08-2008

[eluser]stuffradio[/eluser]
I always do:

Code:
<?php

$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');

?>



What's the practical way to load header and footer - El Forum - 06-09-2008

[eluser]Bramme[/eluser]
Same as armchair samurai... I create a masterviewfile like this

Code:
<?php
//master view - index.php
$this->load->view('header');
$this->load->view($page);
$this->load->view('footer');
?>
And then from my controller

Code:
$this->data['page'] = 'somepage';
$this->load->view('index', $this->data);

Seeing as your data gets transferred to your 'subviews' automatically, it works as a charm!
[/code]


What's the practical way to load header and footer - El Forum - 06-09-2008

[eluser]xwero[/eluser]
I tend to use the template inheritance helper. It make the load->view more meaningful and other people can trace master pages by only looking at view files.


What's the practical way to load header and footer - El Forum - 06-09-2008

[eluser]tjmthome[/eluser]
yes, I agree with Bramme, i found the easyest way is a 'masterview' page (yes i know this maybe breaks a little MVC) but is practical


What's the practical way to load header and footer - El Forum - 06-09-2008

[eluser]JoostV[/eluser]
I use one master template from which I include header, footer, navbar, and so on.

I always seem to have one content template that has to be loaded dynamically. I set this in the controller, before I load the main template, like so:
Code:
$this->data['pagvars']['contentview'] = 'name_of_template';
$this->load->view('template_main', $this->data);

Now, in the template I load this specified template, if it exists:
Code:
if (isset($pagvars['contentview']) && file_exists($pagvars['contentview'] . '.php')) {
    // Contentview exists, so let's load it
    require_once($pagvars['contentview'] . '.php');
}
else {
    // Contentview is not set or does not exist
    show_error ('Problem loading contentview');
}



What's the practical way to load header and footer - El Forum - 06-22-2008

[eluser]mattalexx[/eluser]
[quote author="tjmthome" date="1213046956"]yes, I agree with Bramme, i found the easyest way is a 'masterview' page (yes i know this maybe breaks a little MVC) but is practical[/quote]

How does that break MVC?