Welcome Guest, Not a member yet? Register   Sign In
Best Method?
#1

[eluser]Cloak[/eluser]
Hi there.
I'm new to CodeIgniter, and wondering of the best way to do the following.
How would I go about including a view on the top and bottom of every page? Like a header and footer? I can't figure out if you can autoload views.

Thanks.
#2

[eluser]WebsiteDuck[/eluser]
I have a template.php view like this:
Code:
<?php
  $this->load->view('header');
  $this->load->view($main_content);
  $this->load->view('footer');
?>

and then in my controller when I show the view I do this:
Code:
$data['main_content'] = 'home';
$data['page_title'] = 'Home'; //For use in header.php, to set the title
$this->load->view('template',$data);
That will load the home view with the header and footer
#3

[eluser]Sean Gates[/eluser]
There are several methods depending upon your needs.

1. Extend the controller class and within the __construct() and __destruct() add your header and footer views, respectively. (PHP5 only) Note: this locks you into those header and footer globally, which may be detrimental.
2. Add the header and footer to each controllers __construct() and __destruct(), respectively. (PHP5 only)
3. Add the header and footer to the function index() and create a passthru to all the other functions. (I can provide an example if you need).
4. Create a global view where you can pass the page data as a variable (as @WebsiteDuck mentioned)

Hope that helps!
#4

[eluser]tomcode[/eluser]
You can also work with a to all controllers common class, usually called MY_Controller.

Wiki: How to extend the CI_Controller
#5

[eluser]jedd[/eluser]
Hi Cloak and welcome to the CI forums.

[quote author="Cloak" date="1263115475"]
I'm new to CodeIgniter, and wondering of the best way to do the following.
How would I go about including a view on the top and bottom of every page? Like a header and footer?
[/quote]

Be sure to familiarise yourself with the [url="/wiki/FAQ"]FAQ[/url], and indeed the rest of the [url="/wiki"]wiki[/url].

The FAQ contains the answer to this particular question, and likely the next few questions that you'll find yourself asking as you start to play with CI.
#6

[eluser]Cloak[/eluser]
Hi everyone, thanks for the help!
I've gotten it figured out. I used WebsiteDuck's method.




Theme © iAndrew 2016 - Forum software by © MyBB