• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Load in controller or view?

I have several small view file as parts of the main frame. Example of how the views are loaded in one of the view files:
<? $this->load->view('html_head'); ?>
<? $this->load->view('header'); ?>
<? $this->load->view('menu'); ?>
What I would like to know is if there are any benefits or down sides if I load the view files in the controller, like this:
$this->load->view('html_head', $this->data);
$this->load->view('header', $this->data);
$this->load->view('menu', $this->data);
Or is there an even nicer way of doing this view loading? I'd like to move the loading to the controller, but then the designer feels that it hard to tell whats going on... Undecided

Chances are you will be writing a lot of methods, and to type in the same 3-5 lines in every method just to get the header, menu, sidebar, footer, and content to show up just doesn't make sense.

One way to handle this is to create a view, we'll call it template.php:
You can load views within views...

echo $content;


In your controller, you would only have to call that view once, and pass the content variable to it:

$data['content'] = 'woohoo content goes here!!';
$this->load->view('template', $data);

For a slightly different approach to Nate's, watch this video from Nettuts. It's the same thing except the template view loads the content view dynamically (so you can still have individual views that make up the content area.) Just make sure that the variable that gets passed to the template view cannot be injected or this method could possibly be a little bit insecure.

@Nate - Thats exactly how I'm doing it now, didn't knew it was called template system. I'll stick to it.

@bretticus - Gezz louise, hes addressing a lot of stuff in his video. Tnx for the link!

tehehe....I admit I was turned onto this method by watching that nettuts video. My last 2 or 3 posts have had a link to that video.........i think i'm going to put some links in my sig for that series...it rocks...truly.

Ya I am waiting for day7


[quote author="überfuzz" date="1253095061
@bretticus - Gezz louise, hes addressing a lot of stuff in his video. Tnx for the link![/quote]

Ha, yeah after posting I thought, "I should have given him the time to fast forward to."

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.