Welcome Guest, Not a member yet? Register   Sign In
Quick question: Where to put form/table helper?
#1

[eluser]johnnytoobad[/eluser]
I'm a new users still getting my head around MVC. I have a quick question, how much of the code for generating things like tables and form elements should reside in the view file?

Take for example the HTML Table class: http://ellislab.com/codeigniter/user-gui...table.html

Is it good practice to place all that code in the view and have the controller pass the data, or should the $data array be built in the controller and passed to the view. Or should all of it be done in the controller and passed to the view and just echoed?
#2

[eluser]Nicholai[/eluser]
The model does all of your data manipulation.
The controller sends off requests to the model to push in or pull out data.
The controller handles business logic (any server-side validation of form fields, calculations, preparing data to be inserted/updated, etc).
The view is primarily for presentation.

Say you want to build an HTML table of cities. The controller does something like this:
Code:
$data['cities'] = $this->my_model->get_cities();

The model gives the results back to the controller. You load the view:
Code:
$this->load->view('my view', $data);

In the view, you create the loop that presents the data as table rows:
Code:
<table>
&lt;?php foreach($cities as $city): ?&gt;
<tr><td>
&lt;?php echo $city->Description; ?&gt;
</td></tr>
&lt;?php endforeach; ?&gt;
</table>

I've only been using CI for three months, but I've found the MVC paradigm really nice for separation of design, application programming, and database programming. It's harder for a web designer messing around in a view to accidentally destroy your application if you're giving him fully-prepared data and not exposing him to the guts of an application.

EDIT: Sorry, I didn't notice you meant the actual CI table class. I think it would be best to let the controller generate the table (and assign CSS classes), then send it into the view so all of the design can be done via CSS.
#3

[eluser]johnnytoobad[/eluser]
Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB