Welcome Guest, Not a member yet? Register   Sign In
Best practice in loading multiple views
#1

[eluser]mihaibaboi[/eluser]
Hi guys, this is a little tough to explain, especially since this problem was explained to me by someone else, so I don't have a code snippet right now.

It goes like this: We have a view that's loaded standard; we'll call it index.php. That index page needs to have one or more photo galleries. Now, it would make sense to have a separate view containing the gallery code, and load as many instances of at as we need.

However, I see a problem in this approach. This would mean loading a view, from inside a view.

Inside of index.php we would have
Code:
$this->load->view('gallery');
I think this kind of goes against the MVC pattern, because views should be loaded via a controller.

So what am I missing? Is there a different way to do this? Is this the right way? Any suggestion is welcomed.

Thanks
#2

[eluser]tonanbarbarian[/eluser]
there is nothing that says that one view cannot load another
years ago we found some code in the wiki i believe that was a helper called partials.
this allowed us to have partial views and we could load them by calling the partial helper function
basically it was just a wrapper around $this->load->view(), however it looked neater in the code to have
Code:
partial('users/sidebar', $data);

not sure if the code still exists or not, but it is easy enough to implement something similar
#3

[eluser]WanWizard[/eluser]
Or have a look at one of the template libraries, like those from Colin Williams or Phil Sturgeon.
#4

[eluser]mihaibaboi[/eluser]
@tonanbarbarian

Thanks for the tip. I'll have a look in the Documentation to see if I can still find the code.

[quote author="WanWizard" date="1289541483"]Or have a look at one of the template libraries, like those from Colin Williams or Phil Sturgeon.[/quote]

I'm already using a Template library. More specifically, this one (I think it's Colin Williams'), I did't realize that it had a relevance. If there's a way to make use of it, please share (the concept, not necessarily the code).

Thanks
#5

[eluser]nuwanda[/eluser]
Loading views within views is just fine.

In fact, that's how templates work.

Load a master view which then loads a header, the content (maybe some sidebars or widgets) and then the footer.

No worries.

Install Wordpress and look at a template. That's how you do it.

In your controller:

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

Then in my_view:

Code:
$this->load->view('header')
$this->load->view('content')
#6

[eluser]mihaibaboi[/eluser]
@nuwanda

Now that I think about it, your right. I never thought of that. Thanks for the tip :-)
#7

[eluser]nuwanda[/eluser]
Well, it's even better than that.

I got this from Jeffrey Way in a Nettuts tutorial.

In your controller you load the view as per normal with your data:

Code:
function index(){
  
    $this->data['content']='content_view';
    $this->load->vars($this->data);
    $this->load->view('template');
}

That loads a template view:

Code:
$this->load->view('header');
$this->load->view($content);
$this->load->view('footer');

See, the header is common to all views, so is the footer. You've passed the content view as a separate view which gets loaded between the two of them.

The thing is, the header and footer are the same for all views. But the content is specific to your controller. All you need to do is create different content views. they can be anything: login views, signup views, etc.
#8

[eluser]mihaibaboi[/eluser]
Yeah, I use something similar. It's the Template Library (you can find the link a few posts up). It does kind of the same thing, but a little more structured.
#9

[eluser]nuwanda[/eluser]
If you need it. I say keep things lean.




Theme © iAndrew 2016 - Forum software by © MyBB