Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who's responsibility is it to encode a string?

Who's responsibility is it to encode a string, the model or the view?

I've a string from a database, it's come from the user - I want to keep as a much information as possible in the database, so I'm saving the input verbatim.

When I come to display the string, should I be encoding it when I populate the view model, or should the view decide if it wants to display it encoded or not?

Thanks,
K

like image 224
Kieron Avatar asked Aug 27 '09 18:08

Kieron


2 Answers

The View

The difference between the two is that The Model holds that data and The View is responsible for showing the data based on the output medium. Because if you wanted to transmit this data over some none HTML medium you probably don't want it HTML encoded.

like image 77
Nick Berardi Avatar answered Oct 04 '22 06:10

Nick Berardi


I think this mostly pertains to how clean you'd like your View. If you encode on the view end you keep your controller free of data manipulation operations while muddling the view up with ugly script tags, however if you do it on the controller end you'll have a cleaner (more designer friendly) view and possibly confusing code in the controller.

The real question is would you rather muddle your back-end code or the view markup?

Generally speaking though I believe the best practice would be in your view so that your controller has the ability to morph to different output streams without changing how it operates.

like image 29
Nathan Taylor Avatar answered Oct 04 '22 07:10

Nathan Taylor