Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twig and quote character

Tags:

twig

symfony

in a twig template I want to insert javascript text like this :

<script type="text/javascript">
    {{ "var applicationBundleName = '" ~ application_bundle_name ~ "';" | raw}}
</script>

When the html is rendered, I have this :

<script type="text/javascript">
    var applicationBundleName = &#039;MyBundle_name&#039;;
</script>

While rendering the quotes are replaces by their html entities => javascript error

How can I say not to remplace special characters by their codes ? or maybe there is a nicer way to do that..

Thank you

like image 921
mlwacosmos Avatar asked Apr 02 '13 13:04

mlwacosmos


1 Answers

You have to quote only the twig variable:

<script type="text/javascript">
    var applicationBundleName = "{{ application_bundle_name|raw }}";
</script>

I'm not sure but I think that if the variable is a string you don't need to use de raw filter

like image 140
Erioch Avatar answered Oct 03 '22 00:10

Erioch