Reputation: 41
Say I want to retrieve and display a list of dates to the user for a given input. The Controller takes the input, queries the Model for the dates (returned in Unix timestamp format from the DB), and then passes the dates to the View for display.
My question is: where in this chain should I be reformatting the date to something human-readable? i.e. "1323473367" reformatted to "Dec 9, 2011".
On the one hand, it makes sense to me to do it in the Model, to keep the Controller as 'light' as possible and keep the View as purely a template as possible. On the other hand, a date format is sort of a presentation detail... so maybe it belongs in the View. Or maybe it belongs in the Controller, since it's not really part of either the Model or the View.
Upvotes: 4
Views: 152
Reputation: 4628
Excellent conceptual question.
Ideally, this should be done in the view. In my applications, I use JavaScript to generate an interface. The user interacts with the interface and the interface maps these interactions onto Ajax requests. The server returns timestamps (instead of formatted dates) and the interface maps these timestamps onto formatted time strings. The model only "understands" timestamps and is apathetic to how humans understand time. The controller doesn't "understand" anything.
However, CodeIgniter doesn't work that way. Instead, the controller requests information from the model, uses the view to render that information, and returns the view to the client. The view is just a template for rendering information. It can't translate information into another form the same way JavaScript can.
Important principle: the controller must be apathetic to business logic. The meaning of time is part of the business logic. Therefore, the controller shouldn't "understand" what a timestamp "means", which is necessary for the controller to translate a timestamp into a human readable string. This leaves the view and the model.
As stated, the view doesn't "understand" information; it only puts HTML tags around it.
All that's left is the model. When the client requests a resource, it should tell the server its locale. The controller should pass the locale to the model and the model should "understand" the locale and format the time string accordingly.
Upvotes: 0
Reputation: 19999
You could add a utility class for formatting dates. So before your pass the values to the view (after pulling from the model) you can format the timestamps as desired, yet keep the functionality in a centralized, modular fashion.
Upvotes: 1
Reputation: 1306
It's a kind of a phylosophical question. I would probably put it inside a view, because date formatting is something that could vary from one design to another. But it's your call, really.
Upvotes: 0