javascript- django jslint:
They can be easily hidden in javascript comments, just as Anton Kovalev suggested:
console.log("Conditional log");
String variables
The obvious solution is to include the django tag in double quotes. However, it is worth remembering that the generated code may be invalid if the contents of the variable are not properly escaped:
var javascript_var = "{{ context_var|escapejs }}";
Complex structures serialized in json
Edwin's trick works:
var javascript_var;
/* {{ '*' + '/' }}
javascript_var = {{ context_json_string_var }};
// */
If you included the original javascript template in the html template, you can declare the getter function here and access it from the javascript script:
<script type="text/javascript">function getIt() { return {{ context_var }}; };</script>
<script type="text/javascript">{% include 'script.js' %}</script>
In that case, the javascript source would look pretty acceptable:
var haveIt = getIt();
source
share