views:

108

answers:

2

Hi,

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

+13  A: 

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.

Nick Berardi
Also, encoded data in the database is junk.
peacedog
+2  A: 

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.

Nathan Taylor