• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping through views

#1
[eluser]Unknown[/eluser]
Hi, I'm building a simple webapp using CI and I've come to a question about best practices and performance when loading views.

Let's say I want to loop through all the comments for an article, I'd normally do something like this in my view file:

Code:
<?php foreach($comments as $c) { ?>
  <div class="author">&lt;?php echo $c->author; ?&gt;</div>
  <div class="message">&lt;?php echo $c->message; ?&gt;</div>
  <div class="date">&lt;?php echo $c->date; ?&gt;</div>
&lt;?php } ?&gt;

Now, I'll probably have a much more complex code for each of the comments, so I wold like to abstract that piece of the code into another view file, and do something like this instead:

Code:
&lt;?php foreach($comments as $c) {
  $this->load->view('comment_view', $c);
} ?&gt;

But is it a good practice to load views in a loop? Maybe I can call a library method or a helper function instead?

I hope someone can help me with this.

Thank you!
Maxi

#2
[eluser]Cristian Gilè[/eluser]
From the user guide: CodeIgniter will intelligently handle multiple calls to $this->load->view from within a controller.
Code:
$this->load->view('view_name_1', $a);
$this->load->view('view_name_2', $b);
$this->load->view('view_name_3', $c);


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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