Welcome Guest, Not a member yet? Register   Sign In
loading a view file within another view file
#1

[eluser]eagleon[/eluser]
Hi everyone,

I was hoping someone could help me with this. I read the manual but could not find the answer there. Alright, so if I wanted to include another view file within a view file, how would I go about doing it? Basically, how do I replicate an include()?

I already know how to load it like this within the view file:

Code:
$this->load->view('welcome');

But I don't want to have this sort of code in a view file. Another option I considered if I have no choice, is to use the template parser. But even the template parser does not have a documented method of doing this. I know I can simply dump the contents of my target view file to the $data array using file_get_contents() or something, but this does not feel like a pratical solution to me.

Is there something within the framework that is designed specifically for this task?

All help is much appriciated!


Thanks! Smile
#2

[eluser]jalalski[/eluser]
I may be misunderstanding, but can't you just use 'include(somfile)'?
#3

[eluser]eagleon[/eluser]
You haven't misunderstood, I can do a regular include()... and if there is no other option, this is what I would probably end up doing or, i'd just use $this->load->view(); ... but I was hoping there was a built in CI way of passing a view file though the data array (by spcifying the view file in the controller file ONLY), and then including it by means of each $variable. I want to do this because I want a very strict seperation between the presentation and everything else. Makes sense?
#4

[eluser]OwanH[/eluser]
Quote:I know I can simply dump the contents of my target view file to the $data array using file_get_contents() or something, but this does not feel like a practical solution to me.

Your right, that isn't a practical solution and it's certainly not the way of the CI coder young warrior Smile. You can use $this->load->view(...) to return views as data. For example:

Code:
$this->page_data['content'] = $this->load->view('forms/login', $form_data, true);

In the above code snippet, I am changing the behaviour of $this->load->view so that it returns the contents of the view file 'forms/login.php' as a string rather than sending it to the browser. The key element here is the third parameter passed to $this->load->view, which is specified as true. The default value is false, which sends it to your browser.

Hope this helps. FYI also, see the docs, http://ellislab.com/codeigniter/user-gui...views.html. It's explained as well in the last section of this docs page.
#5

[eluser]eagleon[/eluser]
THANK YOU THANK YOU THANK YOU!

This certainly helps, and is exactly what I was looking for. I knew there had to be a builtin way, and this makes complete sense. I am a little embarassed that I missed it in the manual. I read over the views manual page a couple of times.. I guess this is a good warning sign to get some sleep lol!

Thanks again OwanH Smile
#6

[eluser]OwanH[/eluser]
LOL your welcome eagleon, I know how it goes, many times I too I have overlooked the clearest and simplest of things when burning that ever popular midnight oil. Anywayz, glad I could help and all the best with your CI adventures Smile.
#7

[eluser]RaF007[/eluser]
What about just using $this->load->view('login/form') in the view file? Is this wrong (from an MVC point of view)?
#8

[eluser]OwanH[/eluser]
Quote:What about just using $this->load->view(‘login/form’) in the view file? Is this wrong (from an MVC point of view)?

It's not so much a matter of whether doing this is wrong or right, it's more a matter of your development approach. It's perfectly OK and feasible to have a view file loading another view file, i.e. views within views. In some cases however you may want to keep the tasks of loading your views within the controller methods. To give an example, the line of code I highlighted above,

Code:
$this->page_data['content'] = $this->load->view('forms/login', $form_data, true);

is actually a line of code from a webapp my business is developing. To explain the logic, I have a controller called User and there's a method called _login_page that loads the login page if the user if not logged in and they attempt to access the app. Now each page requires multiple views to be loaded, there's a header view, menu view, content view, and footer view. So my controller method handles the loading of all these views, using models and libraries to access data from the app's database and passing it on to the views. Where there is a need to load sub-views within any of these views, the controller method loads the sub-view, assigns it to a data variable so that the entire contents of the sub-view are passed to the view as data that the view can just output.

Now this approach suits my business' app fine because I have no more than two hierarchies of views when loading a page request, and for Ajax-based requests, no more than one hierarchy. If you need to go deeper than two hierarchies of views then you may prefer to load sub-views within the view files, although even then you can still delegate all the loading to the controller.

Like I said it's a matter of your development approach and what you or the development team may consider to be a best practice.
#9

[eluser]jalalski[/eluser]
@OwanH: That's a nice approach. I'd completely forgotten about the "third parameter"...
#10

[eluser]eagleon[/eluser]
Yep, it's just like OwanH said. That's why in my case, even though I was aware of the $this->load->view(), I didn't use it since my development approach is against such pratice. Just to clarify on what that means for me, my view files need to strictly contain presentation data only. I do all the work (everything) in the controller. This way, if my company later gets a designer down the road who knows nothing about PHP, all he/she needs to learn is how to echo and they are good to go. Smile Plus, I don't have to worry about anyone messing up the back-end so to speak. The only PHP my view files will ever contain is <?= $var ?> ... I even restrict my loops to the control structure whenever possible.

P.S. OwanH, I finally got some sleep. lol




Theme © iAndrew 2016 - Forum software by © MyBB