mlwacosmos
mlwacosmos

Reputation: 4541

twig and quote character

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

Upvotes: 5

Views: 6811

Answers (1)

Erioch
Erioch

Reputation: 359

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

Upvotes: 6

Related Questions