Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC: Where should I format data?

I get data from Model (an array with data) and I need to display with a specific format. I need to iterate over the array, format the data and then display it. Where should I format data to display? In Model, Controller or View? Thank you.

like image 216
jared Avatar asked Sep 14 '10 12:09

jared


6 Answers

Iteration over an array and displaying the data is done in the view. Therefore I would also do the formatting in the view. If formatting is complicated and/or requires a lot of code, put it in a helper function.

For example:

View:

<?php foreach($array as $item): ?>
    <p><?php echo format_function($item); ?></p>
<?php endforeach; ?>

Helper:

function format_function($text)
{
    // Do some formatting here...
    return $formatted_text;
}
like image 127
Mischa Avatar answered Oct 04 '22 18:10

Mischa


if your viewdata has data from different models, or has only a select part of 1 model, you could make a ViewModel, which you then could map with Automapper.

ViewModels have several advantages. They are clear to work with, unclutter your data, can add safety,...

like image 41
Stefanvds Avatar answered Oct 04 '22 17:10

Stefanvds


You can do it in View.Not in model In View you can do specific operation(converting/conditions/)

like image 20
Oyeme Avatar answered Oct 04 '22 18:10

Oyeme


If you working on a bigger project, I would suggest you to have an extra layer or class that is responsible for transforming your object (i.e. domain model object) into a data transfer object (view model object).

Otherwise apply the suggestions of doing the formatting in the view :)

The transforming could be involved with formatting strings, decimals (currency), datetimes and so on. Also transform an object graph (have a look at my example) into a flat DTO is possible.

The controller would be responsible for calling the mapping algorithm.

So in the view you do not have to iterate over references of your object. Instead you use a flat well formatted view model.

Your view will not cluttering up and looks very clean.

A tool that does this transforming job is available in the .NET world. It is called AutoMapper. Perhaps there is an equivalent in PHP.

Here is an example

This is an object model: alt text

You could transform it into this smart view model:

alt text

The advantages of this approach:

  • seperation of concerns

  • clean view

  • No code duplications, i.e. formatting a datetime in each view. (Don't repeat yourself!)

The disadvantages of this approach:

  • expensive for the beginning, so little projects do not profit from this approach
like image 23
Rookian Avatar answered Oct 04 '22 19:10

Rookian


Presentation != data formatting. Please consider the following example:

A international shop with has a product page, with various information regarding product dimensions, etcetera. Because of the international nature of the shop, this data should be formatted differently for each locale the shop is visited in. For example: In Europe, measurements are shown as metric values, whereas US clients see the same data formatted as imperial values.

An important note is that this particular type of data shouldn't be stored multiple times for each format, though data regarding prices for example should. This is because of the fact that product prices do differ per locale. Measurements and dates on the other hand are universally equal across different locales; only the way they are displayed and formatted differ. Information should always be stored with as little redundancy as possible.

The view portion of such an shop (or any MVC-based application, for that matter) shouldn't do anything else besides rendering the data and determining how this data is presented to the user. The data itself shouldn't be changed by the view in any way. This is exactly why information regarding measurements and time should be stored in a ISO standardized format, which makes formatting the data to other formats more easy. Measurements should be stored as metric values for example. The actual formatting of data per locale should take place in the model after the dataset is retrieved from the database, preferably with a statically accessible Helper-type class for the most flexibility. After the data is formatted, it's returned to the controller which then returns it to the current view.

Another important upside of this way of handling data formatting, is that your data still will be correctly formatted when you try and get the dataset via a view-less action (ie. a JSON object retrieved via AJAX). Data that is sent back to the client in any way (via a "normal" HTML template or as an JSON/XML string) shouldn't differ; only the way it is presented.

like image 34
Max van der Stam Avatar answered Oct 04 '22 19:10

Max van der Stam


Do it in your View as it is responsible for the presentation.

The reason for

  • not doing it the Model is that you can then render different views using the same data (otherwise you need to create different set of data),
  • not doing in the controller is because it is not the Controller's responsibility

See MVC background reading

like image 24
Syd Avatar answered Oct 04 '22 19:10

Syd