From a Django template, I would like to include an html snippet from a file, say mysnippet.html
:
<div>
blah
</div>
into a javascript variable:
<script type="text/javascript">
var myvar = {% include 'mysnippet.html' %}
</script>
The problem is that the new lines would need to be escaped. Otherwise, Javascript complains for "unterminated string literal".
I know slashes can be added with {{x|addslashes}}
but I don't know how to do it for the {% include %}
tag.
have you tried filter
template tag?
{% filter addslashes %}{% include 'mysnippet.html' %}{% endfilter %}
better escape of all special characters is to use escapejs
filter:
{% filter escapejs %}{% include 'mysnippet.html' %}{% endfilter %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With