why should I use content_tag instead of "regular" html?

html, ruby-on-rails, ruby-on-rails-3

Solution

Being able to use the content_tag helps when you are programmatically generating HTML inside of ruby code, e.g.: helpers and presenters.

In my opinion, I would not normally use content_tags in the layouts and templates of a project -- I don't think it helps coding readability. I do not see any gains for productivity or performance here.

HOWEVER: word of advice: if that's what your team has standardized on -- go with the team.

Problem

I know that there is an option to use the content_tag function in ruby-on-rails, which helps to generate an html tag. Some rails developers in a company I work with told me that this is the "convenient and proper" way and I should not write a "native" html in order to generate a div for example... Is it true? Is it a kind of rails standard? Does it have something to do with performance issues or render speed? I attach the codes for my previous code: ``` <div class="alert alert-<%= key %>"><%= value %></div> ``` and the rails function usage ``` <%= content_tag(:div, value, class: "alert alert-#{key}") %> ``` The first looks to me pretty, understandable and intuitive - more than the second code.. What do you think about that?

Original source