2

How can I display an entry string, with included special characters (like single and double quotes) , inside a script tag without translating these special characters into html entities but escaping their quotes?

By default: Carlo's "string" will output Carlo's "string" which in javascript if the variable is shown on screen will show all the characters of the html entities.

I have tried:

<script>
   var string = '{{ creatif.creatifsName|raw }}'
</script>

Which removes the html entities but obviously the script will not work because the quotes are not escaped.

1 Answer 1

3
<script>
   var mystring = '{{ creatif.creatifsName|escape('js') }}'
</script>

https://twig.symfony.com/doc/2.x/filters/escape.html

Remember to wrap your variable declaration in quotes for it to be set as a string in the JS.

1
  • Great, thank you so much!!!
    – Samuel
    Commented May 3, 2022 at 15:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.