Welcome Guest, Not a member yet? Register   Sign In
Formatting of HTML: View or Controller
#1

[eluser]prezet[/eluser]
Hi

Just a general question, if I have an array of data, and I wanted out output this array in the form of an html unordered list as so:

Code:
$output = '';

foreach( $dataItems as $dataItem ){
   $output .= '<li>'.$dataItem.'</li>';
}

$output = '<ul>'.$output.'</ul>';

Should this type of formatting be done in the controller, and passed to the view, or done directly in the view??

Cheers
#2

[eluser]xwero[/eluser]
the html helper has a ul function for this sort of thing if you want to keep the view clean. If you think designers are going to change it you better loop it in the view.
#3

[eluser]prezet[/eluser]
Is it better MVC practise to do this in the controller? Or is using the view acceptable?
#4

[eluser]Référencement Google[/eluser]
I would say that when it's time to talk about HTML or user output, it's time to think of a view. That said, we all know it's not always possible to keep PHP 100% free of HTML code, sometimes it happend, but when you can avoid to have that situation, then do it.

As for your example in your first post, I would do that in a view since it is user output related.
#5

[eluser]Dready[/eluser]
As always data in the controller and html in the view...

views/ul.php
Code:
<ul>
{items}
<li>{label}</li>
{/items}
</ul>

controller :
Code:
$tpl = array ('items'=> array (
                  array('label'=>'something'),
                  array('label'=>'something else'),
                  array('label'=>'and so and so')
));

$this->load->library('parser');
$this->parser->parse('ul',$tpl);
#6

[eluser]Colin Williams[/eluser]
I've yet to find much of any reason to put HTML in the controller. You can just load views in a loop and pass the results on to a broader view that contains the list of view outputs.




Theme © iAndrew 2016 - Forum software by © MyBB