Welcome Guest, Not a member yet? Register   Sign In
Multiple view help
#1

[eluser]Unknown[/eluser]
Hi, long time lurker, first time poster. :cheese:

I know CI is capable of displaying multiple views called within a controller (according to the user guide - which by the way is excellent), but I have yet to get it to work. If I use the following code taken directly from the user's guide:

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

}
?>

...only the last view called gets displayed. In the above code example, I would only get the "footer" view displayed to the browser...none of the views before it gets displayed. Any ideas why? As a work around, I just call the other views from within a parent view. Although, it would be nice to call multiple views from the controller. I've also tried auto-loading a library class which calls separate methods for each view...no luck.

Not sure if it matters, but I'm using version 1.5.4 of CI, Apache 2.0 running PHP4 (latest version prior to 5).

Thanks in advance for any help!
#2

[eluser]jwindhorst[/eluser]
I use that all the time, and it looks like you are using it properly. Here is the code that is working for me.
Code:
function index()
  {
    $this->load->view('header');
    $this->load->view('home_page');
    $this->load->view('footer');
  }

Try viewing your source to make sure there is not some other syntax error causing things to fail maybe?

Anyway, if anyone out there finds a way to automatically load the header first, the whatever I tell it to load, the automatically load the footer, that would be VERY cool! I hate having to call the header and footer in on every method of every controller.
#3

[eluser]Pascal Kriete[/eluser]
@tyzy808, I hesitate to say welcome Wink . It's your CI version that's causing trouble here. Multi-view loading wasn't introduced until 1.6 .

@jwindhorst, if you're only loading one view in between - create a simple helper/library.
Code:
// helper example
function load_view_with_template($view = '', $data = array())
{
    $CI =& get_instance();

    $CI->load->view('header');
    $CI->load->view($view, $data);
    $CI->load->view('footer');
}

You can also nest views, so you could create a 'template' view that you load. The possibilities are endless!
#4

[eluser]jwindhorst[/eluser]
Inparo,

I hadn't even thought of using a helper for it, so stoopid simple! Of course my favorite answers usually are!

Now let's take it to a slight higher level:
Code:
// helper example

// Now we can load in as many as we want.
$view = array('first_view'=>array('data in here'), 'second_view'=>array('data in here'));

function load_view_with_template($view = array(), $data = array())
{
    $CI =& get_instance();

    $CI->load->view('header');
    foreach($view as $v=>$d)
    {
        $CI->load->view($v, $d);
    }
    $CI->load->view('footer');
}

This exands the functionality to allow for multiple views each pulling in their own data sets.

Thanks for the heads up, I'm think I'm going work on implementing this right now!
#5

[eluser]Unknown[/eluser]
Inparo, thanks for the heads-up on the version issue i had...well, still have since i can't update the version until my current application ends it live run. I suspected thats what it was...but of course was too lazy to read the part of the documentation on version features. DOH! :red:

jwindhorst, thanks for the reply. I'm sure Inparo's suggestion will work for you... it's what i do as well. Just make sure you have the latest version...unlike some people i know (me) ;-)

Thanks again!
#6

[eluser]Pascal Kriete[/eluser]
My pleasure.

@jwindhorst looks good Smile .




Theme © iAndrew 2016 - Forum software by © MyBB