VSCodeBeautify icon indicating copy to clipboard operation
VSCodeBeautify copied to clipboard

VSCodeBeautify has deleted Django tags within a <scritp> section, in a Django html template file.

Open stevechenzy opened this issue 4 years ago • 2 comments

in a Django html template file, there is a section of


<script>

  var ctx = document.getElementById("chart_revenue").getContext("2d");
  var myChart = new Chart(ctx, {
    type: "bar",
    data: {
      labels: {{ data_regions | safe }},
      datasets: [
        {
          label: "营收",
          data: {{ data_revenue | safe }},
          backgroundColor: "green",

          borderWidth: 1,
        }
        ],

    },
    options: {
      scales: {
        yAxes: [
          {
            ticks: {
              beginAtZero: true,
            },
          },
        ],
      },
    },
  });

</script>

After formatting, the result is:


<script>
    var ctx = document.getElementById("chart_revenue").getContext("2d");
    var myChart = new Chart(ctx, {
        type: "bar",
        data: {
            labels: {
                {
                    data_regions | safe
                }
            },
            datasets: [{
                label: "营收",
                data: {
                    {
                        data_revenue | safe
                    }
                },
                backgroundColor: "green",

                borderWidth: 1,
            }],

        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true,
                    },
                }, ],
            },
        },
    });
</script>

which is wrong.

stevechenzy avatar Jul 09 '20 08:07 stevechenzy

I have the same issue - is there a way one can have Beautify skip formatting inbetween tags?

EDIT - BETTER WORKAROUND TO SEPARATE HTML and JAVASCRIPT I've learned of: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#json-script which is the new recommended way of passing data to javascript.

It does turn it to two lines of code however you can have {{ value|json_script:"hello-data" }} in your html

then just load a javascript file (below the above line) with the following embedded in it: const value = JSON.parse(document.getElementById('hello-data').textContent);

ihelmer07 avatar Aug 31 '20 01:08 ihelmer07

@ihelmer07 It is possible to skip formatting inbetween ? How?

7iomka avatar Feb 24 '21 01:02 7iomka