Welcome Guest, Not a member yet? Register   Sign In
What's the practical way to load header and footer
#1

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

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

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

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

Not sure if it's the best.
#4

[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');
#5

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

Code:
<?php

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

?>
#6

[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]
#7

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

[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
#9

[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');
}
#10

[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?




Theme © iAndrew 2016 - Forum software by © MyBB