0

I need a specific format that I build in my PHP and which is then sent to my view and the JavaScript (using Symfony, but this is not a Symfony issue).

The problem is specified in the title: the ' from the string is transformed into my js and I don't want it to be transformed

Here are how it looks like

PHP side

        case "01":
            $dynamic_month="['Février ".$previous_year."','Mars ".$previous_year."','Avril ".$previous_year."','Mai ".$previous_year."
            ','Juin ".$previous_year."','Juillet ".$previous_year."','Août ".$previous_year."','Septembre ".$previous_year."','Octobre 
            ".$previous_year."','Novembre ".$previous_year."','Décembre ".$previous_year."','Janvier ".$year."']";
            break;

This is building a string with something like ['Month Year','NextMonth Year'] (this is what a var_dump is printing.

But in my JavaScript (chart.js inside a twig view). The ' are replaced.

var chart = new Chart(ctx, {
                // The type of chart we want to create
                type: 'line',

                // The data for our dataset
                data: {
                    labels: {{ dynamic_month }},
                    datasets: [{
                        label: "Connexion / mois",
                        backgroundColor: 'rgb(255, 99, 132)',
                        borderColor: 'rgb(255, 99, 132)',
                        data: connectedByMonth,
                    }]
                },

                // Configuration options go here
                options: {}
            });

The issue is on the line

labels: {{ dynamic_month }}

this is replaced once compiled by this

labels: ['Février 2018','Mars 2018', et caetera et caetera

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Minirock
  • 628
  • 3
  • 13
  • 28

2 Answers2

0

take a look at this escaping quotes: Escaping quotation marks in PHP

if that doesnt work, you could always do a str.replace after the fact and properly convert it back

Sam Alexander
  • 504
  • 1
  • 6
  • 24
0

You are setting the dynamic_month variable on the PHP side, but using it in Javascript; you'll need to replace the:

labels: {{ dynamic_month }},

With

labels: {<?php echo $dynamic_month ?>},

HTH

neophytte
  • 648
  • 2
  • 11
  • 25