Dorna96
Dorna96

Reputation: 1

using Django Template Variables in javascript

I'm writing an app that needs dynamic javascript. I didn't know how to use Django template variable in javascript. when I searched I found some answeres like Django Template Variables and Javascript but I still have the problem. when I write this piece of code in my html :

        <script>jQuery(document).ready(function ($) {
           $(".nav").css({"opacity": "0.5"});
           $("#description").animate({opacity: '+=0.5'}, 10000);
           });
        </script>

every thing is fine. the navbar is transparent and <p id="description">{{description}}</p>is shown by jquery animate function. but when i change it to this:

      <script>jQuery(document).ready(function ($) {
           $(".nav").css({"opacity": "0.5"});
           var a = "{{description}}";
           $("#description").animate({opacity: '+=0.5'}, 10000);
           });
      </script>

the navbar is no more transparent and the description is no more shown. what is the problem ?

p.s : I changed a to a = "{{blah}}"; and there is no problem with it. the problem apears when It is a real template variable.

Upvotes: 0

Views: 201

Answers (1)

dan-klasson
dan-klasson

Reputation: 14230

Use verbatim:

Stops the template engine from rendering the contents of this block tag.

A common use is to allow a JavaScript template layer that collides with Django’s syntax. For example:

{% verbatim %}
     {{if dying}}Still alive.{{/if}} 
{% endverbatim %}

Upvotes: 1

Related Questions