Welcome Guest, Not a member yet? Register   Sign In
view into view (like include)
#4

[eluser]Teks[/eluser]
I am new to CodeIgniter, too, so please, Goyo, confirm with others, that the advice I'm about to give you is correct - it does seem to work for me. I was having the same problem as you, but after a bit of research through the forums here, have found 2 different ways to do what you need. Both solutions seem to be valid, and which one you use depends on what your requirements are:

------------------------------------------------------------------------
SOLUTION 1 - CALL "$this->load->view()" FROM INSIDE YOUR VIEW
------------------------------------------------------------------------
This is the easiest and most straight-forward solution, and will be what you need in 90% of all cases. In your view.php file, you can include another view simply by using "$this->load->view()" instead of 'include()'. So, in your own example, you could probably do it like this:

Code:
<html>
  <head>
    <title>Website</title>
  </head>

  <body>

    <div id="header">
      &lt;? $this->load->view('header'); ?&gt;
    </div>

    <div id="main">
      <p> the main content </p>
    </div>

    <div id="footer">
      &lt;? $this->load->view('footer'); ?&gt;
    </div>

  &lt;/body&gt;
&lt;/html&gt;

Note that whatever data you pass to the main parent view from your controller will be available to all nested views, too. So, let us suppose that in your controller you do:

Code:
$data['copyright'] = '2008, My Company';

Then, in your nested "footer.php" view file, you could use that data like this:

Code:
<p>©&lt;?= $copyright ?&gt;.</p>

----------------------------------------------------------------------------------
SOLUTION 2 - LOAD SUB-VIEWS INTO VARIABLES IN YOUR CONTROLLER
----------------------------------------------------------------------------------
In some special cases, you may wish to load the contents of a sub-view (a 'view snippet') into a variable, in your controller. You can then pass the variable, like any other, to your main view, where it will be displayed (= included).

In your controller, you can assign the contents of the sub-view to a variable using "$this->load->view()', like this:

Code:
//create data to be sent to sub-view
$footer_data['copyright'] = '2008, My Company';

//load sub-view into variable:
$data['footer'] = $this->load->view('footer', $footer_data, TRUE);

//send all data to render in the main view:
$this->load->view('maintemplate', $data);

These steps are similar to what you would have to do, if you were using the Template Parser Class - which in effect would be a third solution!

I hope this helps!


Messages In This Thread
view into view (like include) - by El Forum - 11-14-2008, 05:48 AM
view into view (like include) - by El Forum - 11-14-2008, 06:12 AM
view into view (like include) - by El Forum - 11-14-2008, 06:18 AM
view into view (like include) - by El Forum - 11-21-2008, 11:37 PM
view into view (like include) - by El Forum - 11-22-2008, 07:35 AM
view into view (like include) - by El Forum - 11-22-2008, 08:12 AM
view into view (like include) - by El Forum - 11-22-2008, 08:14 AM
view into view (like include) - by El Forum - 05-08-2009, 10:11 AM
view into view (like include) - by El Forum - 04-24-2011, 09:36 AM
view into view (like include) - by El Forum - 07-05-2011, 09:49 PM



Theme © iAndrew 2016 - Forum software by © MyBB