如何使用'javascript变量作为文本'?

时间:2017-05-15 07:35:07

标签: javascript php symfony twig fullcalendar

很抱歉再次发布此内容,但我在互联网上找不到任何有用的内容。 我正在尝试创建一个日历。我正在使用symfony 3和fullcalendar来创建它。 所以在我的twig课程中,我创建了一个变量:

{% set fff = "" %}

fff变量中添加一些文字,例如{start: "2017-05-17", title: "Take my mom from airport"}, 然后我将fff变量传递给JS:

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

然后,如果我想将此任务{start: "2017-05-17", title: "Take my mom from airport"}, allTask​​s变量添加到日历中。所以我创建了一个函数:

$(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 
         ],
     });
 });

当我在示例中放置代码时,它可以正常工作,但是当我尝试执行以下操作时:

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

这对我没有任何帮助。甚至日历也从我的网络上消失了。 所以问题是我做错了什么? allTask​​s 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)"} [] example example

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

<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)
        ],
     });
 });