multiple views per controller

#1
[eluser]Derek Allard[/eluser]
Allow me to introduce one of the most requested features of our view output. Multiple views.

Code:
<?php

class Page extends Controller {

function index()
{
$data['page_title'] = 'Your title';
$this->load->view('header');
$this->load->view('menu');
$this->load->view('content', $data);
$this->load->view('footer');
}

}
?>

Available now in an SVN near you... please test it out.

#2
[eluser]Pascal Kriete[/eluser]
CHEER!

#3
[eluser]Michael Wales[/eluser]
Nice!

For all who want to get a header/footer on your pages very easily - without retyping this within every controller - just extend the Controller class with a constructor/destructor.


Code example (not tested, but should work if my thinking is correct), ghetto-fied for PHP4 support:

/application/libraries/MY_Controller.php
Code:
class Public_Controller {
  function Public_Controller() {
    parent::Controller();
    register_shutdown_function(array(&$this, "shutdown"));
    $this->load->view('header');
  }

  // Ghetto destructor for PHP4 support
  function shutdown() {
    $this->load->view('footer');
  }
}

/application/controllers/whatever.php
Code:
class Whatever extends Public_Controller {

  function index() {
    $this->data->test = 'This is a test.';
    $this->load->view('home', $this->data);
  }

}

#4
[eluser]Josh Giese[/eluser]
wow, this is some good stuff for sure. Thanks Derek and Michael!

#5
[eluser]Pascal Kriete[/eluser]
[quote author="Michael Wales" date="1200715586"]Nice!

For all who want to get a header/footer on your pages very easily - without retyping this within every controller - just extend the Controller class with a constructor/destructor.


Code example (not tested, but should work if my thinking is correct), ghetto-fied for PHP4 support:

/application/libraries/MY_Controller.php
Code:
class Public_Controller {
  function Public_Controller() {
    parent::Controller();
    register_shutdown_function(array(&$this, "shutdown"));
    $this->load->view('header');
  }

  // Ghetto destructor for PHP4 support
  function shutdown() {
    $this->load->view('footer');
  }
}
[/quote]

Looks very nice, but doesn't quite work for me. Well for one, you forgot to extend the Controller. But even then the shutdown function gets called (what does the & in front of the $this do?), but it doesn't actually load the view.

#6
[eluser]Phil Sturgeon[/eluser]
Good stuff Derek, thats gonna make like a whole bunch easier for the newcomers trying to work out multiple views. The old method was just.... dirty lol!

@inparo: Posted code should never be expected to be "copy and paste" safe. It is posted as ideas and suggestions, use common sense when implementing and if he forgot to add the extender keyword chuck it in yourself or post a complete version for the rest of the gang!

Quote:what does the & in front of the $this do?

This passes the refference of the $this object instead of copying it over. Basically makes it a while bunch quicker and still does the same thing.

#7
[eluser]Pascal Kriete[/eluser]
It was never meant as criticism, I'm sorry if it came across that way. He said it wasn't tested.

I really like the idea and played with it all night yesterday, but still haven't made it to work.

#8
[eluser]pwninja[/eluser]
Sweet, I'll have to grab it from SVN later.

#9
[eluser]Michael Wales[/eluser]
inparo - No offense taken. To be honest, I haven't tested it either (although I will be today as I start work on a new project). I'm PHP5 exlusive though. If you are as well - just use a real destructor rather than the ghetto-one, I'm pretty sure that would work.

I have no experience with the ghetto-destructor, I just pulled the code from the PHP documentation and modified it.

#10
[eluser]mrahman[/eluser]
I was wondering why a controller can only load a single view. finally i thought it's a great idea and it should remain this way. i can load a single view and load dozens inside it. but for some situations, i use
Code:
echo $this->output->get_output();
after each load->view() in a controller. but it looks messy :exclaim:

edit:

YES it was a nightmare when i was new to CI, why my page always contains just a header ??? :lol:


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.