views:

41

answers:

3

You have a blog.

You want certain combinations of symbols (i.e. :), :(, :p ...) to be parsed into emoticons before the post in the blog are printed on the screen.

Should this parsing be a responsibility of the Model, the View or the Controller?

+1  A: 

Do you want to store them parsed or raw?

If the former, the controller or model, depending on how fat or thin you are. If the latter, the view.

Leo
Good question. The texts are stored raw.
Emanuil
Then I would say in the view. In CakePHP I'd probably write a helper to do it.
Leo
+4  A: 

Use a View Helper. The helper should have a function that parses for the emoticons.

Think of this parsing as formatting for output, therefore, it should be handled in the View.

webbiedave
What if you need to do several kinds of parsing: emoticons, urls, emails, bbcode etc. all on one text. Wouldn't that look bad if done in the View?
Emanuil
It's common practice to use several helpers in the view. That is, after all, what they're for. Using these helper helps you to keep the view code clean. For example I often use FormHelper, HtmlHelper, JavascriptHelper, TimeHelper etc. in one view.
Leo
Sure, but I guess you use these helpers on different objects. Wouldn't you be concerned if you have to use multiple helpers on one object (in this case the blog post text)?
Emanuil
@Emanuil: It's bad if the controller is formatting html output. It should only pass raw data, not decide how it should be displayed. That's the view's job.
webbiedave
@webbiedave, when you put it like that it makes perfect sense. Thanks!
Emanuil
You're welcome. Glad to help.
webbiedave
When you say view helpers you just mean models right?
AntonioCS
No, AntonioCS. View helpers are functions/classes made specifically for use in views.
webbiedave
+1  A: 

If you want to store them raw, then it belongs to the View.

A View Helper is one choice. Another would be a custom ob_handler that post-processes the rendered HTML before it is delivered. Since you mentioned, the post might also contain BBCode, you could also consider using a BBCode parser, which would handle the emoticons as well.

Personally, I think emoticons are so useless, that the server shouldnt be bothered with it. So I'd probably put that responsibility in a JavaScript that will parse and replace on page load. Did a quick Google and think you could use http://benalman.com/projects/javascript-emotify/

Gordon
Handling emoticons with JavaScript makes a lot of sense. Thanks for the suggestion and the great answer overall!
Emanuil