HTML content stored in DB shows content with HTML tags but not HTML view

I have stored HTML content with HTML tags in database like <div> </div> or <span> </span>, but after fetching it from the DB it shows the same HTML tags and content but not HTML view. What should I do so that it will show me the proper HTML content, not as the tags I inserted into the DB

I got the solution as phoenix_html provide a function raw/1 which can handle this situation. Found reference from the link https://stackoverflow.com/questions/31560620/how-to-render-raw-html-code-in-phoenix-framework

<div id="contentEditableText">
    <%= for %{"contentText" => contentText} <- @contentText.data do %>
      <div><%= raw(contentText) %></div>
    <% end %>
</div>

Be careful using this with user submitted html. If you haven’t sanitized it correctly and you use raw, things like script tags could let your users inject malicious JavaScript that might run for other users.

2 Likes