How to use javascript variable as text?

Sorry for posting this again, but I cannot find anything useful on the Internet. I am trying to create a calendar. I use symfony 3 and fullcalendar to create it. So in my class twigI created a variable:

{% set fff = "" %}

I fffadd text to the variable , for example, {start: "2017-05-17", title: "Take my mom from airport"}, Then I pass the variable fffto JS:

<script>
    var allTasks = {{ fff|json_encode()|raw }};
</script>

Then, if I want to add this task {start: "2017-05-17", title: "Take my mom from airport"},, allTasks variable, to the calendar. So I created a function:

$(function(){
    $('#calendar').fullCalendar({
         events: [
             //there I should add tasks
             //f.e. {start: "2017-05-17", title: "Take my mom from airport"},
             //this code adds a task into my calendar 
         ],
     });
 });

When I add code, as in the example, it works fine, but when I try to do something like:

$(function(){
    $('#calendar').fullCalendar({
        events: [
            allTasks, //this variable is equal to "{start: "2017-05-17", title: "Take my mom from airport"},"
        ],
     });
 });

. . , , ? allTasks var {start: "2017-05-17", title: "Take my mom from airport"},. , , .

: [2017-05-15 11:40:36] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /[%7Bstart:'2017-05-17',title:'Take%20my%20mom%20from%20airport'%7D]" (from "http://127.0.0.1:8000/home")" at /home/david/task_manager/var/cache/dev/classes.php line 3497 {"exception":"[object] (Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0): No route found for \"GET /[%7Bstart:'2017-05-17',title:'Take%20my%20mom%20from%20airport'%7D]\" (from \"http://127.0.0.1:8000/home\") at /home/david/task_manager/var/cache/dev/classes.php:3497, Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0): at /home/david/task_manager/var/cache/dev/appDevDebugProjectContainerUrlMatcher.php:162)"} [] exampleexample

+4
1

:

<script>
    var allTasks = '{{ fff|json_encode()|raw }}'; // make sure that fff return {'start': '2017-05-17', 'title': 'Take my mom from airport'}
</script>

$(function(){
    $('#calendar').fullCalendar({
        events: [
            JSON.parse(allTasks)
        ],
     });
 });
0

Source: https://habr.com/ru/post/1677148/


All Articles