Rescue HTML text from Mysql database with EJS (Node.js)

Asked

Viewed 563 times

1

I am developing a news portal in Node.js where I save the posts in a Mysql database. As a template engine, I use the EJS.

Use the Tinymce plugin to format the text of posts (use bold, Italic and etc). Tinymce is a robust Web page visual editor written completely in Javascript.

This plugin saves in the database the text already formatted in HTML. For example:

<strong>negrito</strong> e <p>texto em paragrafo</p>

The problem is time to present this content. I query the database and return with the tags, not the formatted text. I’m calling the tinymce the right way:

<script src="tinymce/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>

And it is saving right in the bank. But when I perform the query to view, it shows so:

inserir a descrição da imagem aqui

The view code is this:

<div class="col-md-12">
                <div class="noticia_wrapper">
                    <span class="noticia_autor"><%= noticia[0].autor %></span>
                    <span class="noticia_titulo"><%= noticia[0].titulo %></span>
                    <span class="noticia_data"><%= moment(noticia[0].data_criacao).format('DD/MM/YYYY') %></span>
                    <br />
                    <p class="noticia_resumo">
                        <%= noticia[0].noticia %>
                    </p>                        
                </div>
            </div>

Someone can give a light?

1 answer

1


You need EJS to show this HTML without escaping it. Looking at the documentation the command for this is <%-, so you can do:

<p class="noticia_resumo">
    <%- noticia[0].noticia %>
</p> 
  • My friend Sergio, thank you very much. I have been racking my brain about this for some time now... I confess I didn’t know about the escape and not escape HTML thing, so when I looked at the documentation, I couldn’t find the solution. Thank you very, very much, man.

  • @Rodrigozandonadi great to be able to help. If you want you can click and mark the answer as accepted.

  • Sergio, I tried, but as a new user my vote is not computed... Sorry

  • @Rodrigozandonadi to accept do not need points. It is a symbol under the voting arrows and the score of the answer.

  • @RodrigoZandonadi https://pt.meta.stackoverflow.com/q/1078/129

Browser other questions tagged

You are not signed in. Login or sign up in order to post.