Welcome Guest, Not a member yet? Register   Sign In
MVC - Where should I loop?
#1

[eluser]überfuzz[/eluser]
(Couldn't find anything decent to read about this.) Where should I loop arrays that going to be displayed in the view?

View
From what I understand there shouldn't be any loops and stuff in the view.
Control
If I loop through the array here I have to tinker with HTML-tags in the control.
#2

[eluser]oddman[/eluser]
If you're iterating through data for display, you put it in the view. Period.
#3

[eluser]überfuzz[/eluser]
[quote author="oddman" date="1240405366"]If you're iterating through data for display, you put it in the view. Period.[/quote]
So the iteration is done in the control and a string passed to the view?
#4

[eluser]rogierb[/eluser]
No, you pass a resultset to your view and iterate in your view.
#5

[eluser]oddman[/eluser]
lol no.

You assign the data to the view, then you loop through it to display whatever you need.

Please read: http://en.wikipedia.org/wiki/Model-view-controller
#6

[eluser]xwero[/eluser]
There is a middle ground, loop views.
Code:
$data['partial'] = '';

foreach($rows as $row)
{
   $data['partial'] .= $this->load->view('partial_row',$row,true);
}

Personally i don't like that solution because it calls the view file again and again. I think loops and if else code can be in the view. If you use the alternate syntax for it even designers will be able to figure out what is going on.
#7

[eluser]überfuzz[/eluser]
[quote author="xwero" date="1240406444"]There is a middle ground, loop views.
Code:
$data['partial'] = '';

foreach($rows as $row)
{
   $data['partial'] = $this->load->view('partial_row',$row,true);
}

Personally i don't like that solution because it calls the view file again and again. I think loops and if else code can be in the view. If you use the alternate syntax for it even designers will be able to figure out what is going on.[/quote]

I see what you mean. Actually, that was kind of my concern. When I read about MVC it seems that loops, ifs and else are banned in the view part. But its not very handy to be strict with that working with info in arrays.

Thanx all!!!
#8

[eluser]oddman[/eluser]
[quote author="überfuzz" date="1240406684"]I see what you mean. Actually, that was kind of my concern. When I read about MVC it seems that loops, ifs and else are banned in the view part. But its not very handy to be strict with that working with info in arrays.

Thanx all!!![/quote]

Hi uberfuzz,

This is a safe assumption and I think a common misconception about views. If the conditionals and loops you're using are related to display, it's not an issue. If however, it's concerning logic - then it's in the wrong place.
#9

[eluser]überfuzz[/eluser]
[quote author="oddman" date="1240406841"]This is a safe assumption and I think a common misconception about views. If the conditionals and loops you're using are related to display, it's not an issue. If however, it's concerning logic - then it's in the wrong place.[/quote]

Right on, thanx!




Theme © iAndrew 2016 - Forum software by © MyBB