Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja template renders double quotes or single quotes as ' "

Tags:

python

jinja2

Hi i was trying to populate Google Visualization api in jinja template . I took the sample parameters and passed it to the API but it is converting single and double quotes to ' and & Here is the script :

        <script type="text/javascript">
            //load the Google Visualization API and the chart
            google.load('visualization', '1', {'packages': ['columnchart']});

            //set callback
            google.setOnLoadCallback (createChart);

            //callback function
            function createChart() {

                //create data table object
                var dataTable = new google.visualization.DataTable();

                //define columns
                dataTable.addColumn('string','Quarters 2009');
                dataTable.addColumn('string', 'Earnings');

                //define rows of data
                // answerjson=answerjson.replace("&#39;",'"');
                {% set answerjson1='[["1": "Saturday"], ["6": "Sunday"], ["1": "Wednesday"], ["1": "Monday"], ["1": "Monday"], ["1": "Tuesday"], ["1": "Sunday"]' %}

                dataTable.addRows( {{answerjson1}} );

                //instantiate our chart object
                var chart = new google.visualization.ColumnChart (document.getElementById('chart'));

                //define options for visualization
                var options = {width: 400, height: 240, is3D: true, title: 'Company Earnings'};

                //draw our chart
                chart.draw(dataTable, options);

            }
</script>

Here is the input passed to the API I have put the screenshot because here it is rendered as double quotes

Please help me, what i need to do.

like image 776
shobhit Avatar asked Jan 25 '12 16:01

shobhit


2 Answers

Use the safe template filter:

dataTable.addRows( {{ answerjson1 | safe }} );
like image 171
Alex Morega Avatar answered Sep 19 '22 08:09

Alex Morega


tojson filter render the data in json form:

dataTable.addRows( {{ answerjson1 | tojson }} );

The safe filter can generate errors if you parse to json after. For example, the safe filter gives a string of a json formed by single quotes, while the JSON.parser() function can only have as input a string with double quotes.

like image 36
Dinidiniz Avatar answered Sep 19 '22 08:09

Dinidiniz