view into view (like include) |
[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> 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!
[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).
[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!
[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> 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>©<?= $copyright ?>.</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 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!
[eluser]m4rw3r[/eluser]
Solution 3: Use include @Teks: You're completely correct, but a tip is to use <?php instead of <?.
[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!
[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!!!
[eluser]Rushino[/eluser]
Thanks! Found the solution to my sub-view problems Solution 1 is pretty neat way of doing it !
[eluser]Unknown[/eluser]
Thank you very much Teks! Solution 1 is exactly what I was looking for. |
Welcome Guest, Not a member yet? Register Sign In |