Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML tags in database bad practice or good practice?

Sometimes I need to format specific data or part of it that comes from the database .

For example :

If i have a desc (stored in DB) like this :


HTML 4 has been tweaked, stretched and augmented beyond its initial scope to bring high levels of interactivity and multimedia to Web sites. Plugins like Flash, Silverlight and Java have added media integration to the Web, but not without some cost.


and i wanna to format the last line , change the font and color for example .

What 's the best practice to do this ?

embedding HTML tags in my database ??Is this safe and the best practice or there 's some way to separate the structure layer from the presentation layer from the behavior layer ?

like image 227
Anyname Donotcare Avatar asked Feb 22 '12 07:02

Anyname Donotcare


2 Answers

If you plan to manipulate or search upon the stored data then do not store HTML markup in your database. Imagine that at some point you're asked to change the fonts from Tahoma to Georgia, change <b> tags to <strong> or allow the users to search on the HTML column; and searches for strong end up returning irrelevant information because strong is also a frequently used HTML tag.

Storing HTML markup in your database is also a bad idea if you do not check what is being stored. A malicious script tag such as <script>location = 'http://otherwebsite'</script> is just one naive example.

Ideally you should store the data as-is or use some kind of markup such as (wiki or markdown) to store basic formatting information.

like image 128
Salman A Avatar answered Sep 23 '22 13:09

Salman A


There IS some way to separate the data from the presentation. You keep them separate! If you want to do some formatting on that text that you pulled from the database, go ahead and do that in your application code. Note that structural markup is an entirely different topic from presentation markup (font, color, layout, etc)

http://en.wikipedia.org/wiki/Separation_of_presentation_and_content talks about this very point and makes a clear separation between presentation markup and structural markup in the paragraph under Intended Meaning.

Storing formatting tags in your data generally points to poor separation between the two layers or a data model that isn't sufficient to represent your data properly. As the author is storing data in a database, that might indicate that he has just a single field for holding the "content block" of an article rather than multiple fields for the author, title, body, references, etc. For user input data, we often fall back to a markup inside the user content for designating structure. That happens through "fake" html tags or even real html/xml tags like <h1>, <em>, <a>, etc.

Note that I'm not objecting to structural markup on principle but I would look carefully at why it's required if you're storing it in a database. I am objecting to presentation markup on principle.

like image 34
gfortune Avatar answered Sep 24 '22 13:09

gfortune