2

I'm trying to convert a description, which is a string from my database, into HTML. I'm getting the description with {{ projet.description }}, but it seems that in JavaScript, "description" causes a bug in my script... So I create a div with my description on it, make it invisible, and get it with innerHTML.

Twig code

    <div id="desc">{{ projet.description }}</div>
    <div>
        <script type="text/javascript">
            var desc = document.getElementById("desc").innerHTML|e('js')|raw;
            document.write(desc);
        </script>
    </div>

CSS

#descr {
    display:none;
}

But now, document.write() still returns a string like "<p><em>POKEMON</em></p>". However, I want it in HTML.

1

1 Answer 1

2

I'm almost sure that HTML was escaped, so try it out:

<div id="desc">{{ projet.description|raw }}</div>

See raw filter on twig docs: raw

The raw filter marks the value as being "safe", which means that in an environment with automatic escaping enabled this variable will not be escaped if raw is the last filter applied to it

Also:

var desc = document.getElementById("desc").innerHTML|e('js')|raw;

The above snippet is not valid on twig because it isn't surrounded by valid delimiters such as "{{ }}" or "{% %}".

Sign up to request clarification or add additional context in comments.

1 Comment

Omg thanks for your help ! It works ! I knew that it was because of the HTML when I look at the src code, it wasn't HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.