CodeIgniter Forums
Looping through views - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Looping through views (/showthread.php?tid=52273)



Looping through views - El Forum - 06-04-2012

[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


Looping through views - El Forum - 06-04-2012

[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);