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

[eluser]Goyo[/eluser]
Hi,

my english is bad, but i hope you can understand me! im absolutly new in this framework and MVC model to. I actually develop website in php without framework and i decide to use CI. But i have a few "concept" problems. I really dont know how implemnet some thinks! for example, normally a index page structure are like this:

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

<div id="header">
&lt;?php include("header.php") ?&gt;
</div>

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

<div id="footer">
&lt;?php include("footer.php" ?&gt;
</div>

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

ok, for the example i keep it simple.

I wrote 3 controllers:
main.php -> to manage the entire page view
header.php -> to manage the header view
footer.php -> to manage te footer view

And his 3 views:
main_view.php
header_view.php
footer_view.php

I make it this way because the header has dinamyc content and i need to access to database.

my question is: how i include the header and footer views with his respective controllers?

I hope you understand my question and i know its a concept problem! thanks!
#2

[eluser]Colin Williams[/eluser]
Keep in mind that, with CI, controllers are generally meant to serve URI requests, not nested views. For the kind of setup where controllers do serve sub-views, look at the Modular Extensions - HMVC library (check the wiki or search this forum).
#3

[eluser]Goyo[/eluser]
Colin,

thanks for you fast answer. i research the hmvc library and find a very similar problem on this post

http://ellislab.com/forums/viewthread/64774/

i could be helpfull for someone else with the same concept problem!

i post the solution in a moment!

thanks colin!
#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:
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Website&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;

    <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!
#5

[eluser]m4rw3r[/eluser]
Solution 3: Use include Tongue

@Teks:
You're completely correct, but a tip is to use &lt;?php instead of &lt;?.
#6

[eluser]Goyo[/eluser]
m4rw3r -> include???


ok, first of all thanks Teks for you answer! Solution 1 is correct!!

each partial view has his own controller!!

Include?? this is OO i really dont know if you mean it really.. please, this is a forum! dont confuse anothers!! thanks!
#7

[eluser]Goyo[/eluser]
jajjaa

sorry m4rw3r!! i dont read all you post! jaja! SORRY! i understand you suggest to use include! teks you are on the good way!! i do it the same way! THANKS!!!
#8

[eluser]breastfed[/eluser]
Thank you Teks - this works pretty Cool!
#9

[eluser]Rushino[/eluser]
Thanks! Found the solution to my sub-view problems Smile Solution 1 is pretty neat way of doing it !
#10

[eluser]Unknown[/eluser]
Thank you very much Teks! Solution 1 is exactly what I was looking for.




Theme © iAndrew 2016 - Forum software by © MyBB