0

I am trying to use a jquery extension as Event calendar in my application. I tried it first with core php and it works fine. Now I want to use it in my symfony2 application, but i am not able to display it in my twig file. Here is my code i am using to render but i am not sure where i am doing it incorrect.

Controller

public function studentCalendarAction()
{
  $year = date('Y');
  $month = date('m');
  $response = new Response(json_encode(array(
        array(
                'id' => 111,
                'title' => "Event1",
                'start' => "$year-$month-10",
                'url' => "http://yahoo.com/"
        ),
  )));
  $response->headers->set('Content-Type', 'application/json');
  return $this->render('CollegeStudentBundle:StudentAttendance:calendar.html.twig',array('response'=>$response));
}

Router

CollegeStudentBundle_attendance_calendar:
    pattern:  /student/attendance/calendar
    defaults: { _controller: CollegeStudentBundle:StudentAttendance:studentCalendar }
    requirements:
        _method:  GET|POST

TWIG

    <script type='text/javascript'>

                $(document).ready(function() {

                $('#calendar').fullCalendar({

                    editable: true,

                    events: "../../student/attendance/calendar",

                    eventDrop: function(event, delta) {
                        alert(event.title + ' was moved ' + delta + ' days\n' +
                            '(should probably update your database)');
                    },

                    loading: function(bool) {
                        if (bool) $('#loading').show();
                        else $('#loading').hide();
                    }

                });

            });

    </script>


</head>

<body>
    <div id='calendar'></div>
</body>

Even I tried to make my Router like this

CollegeStudentBundle_attendance_calendar:
    pattern:  /student/attendance/calendar
    defaults: { _controller: CollegeStudentBundle:StudentAttendance:studentCalendar, _format: json }
    requirements: { _format: (xml|json), _method: GET }  

But this displays the code of twig file.

1 Answer 1

3

Just return the created $response, not the rendered response.

Edit: Your revised studentCalendarAction would be,

public function studentCalendarAction()
{
  $year = date('Y');
  $month = date('m');

  $jsonData = json_encode(array(
        array(
                'id' => 111,
                'title' => "Event1",
                'start' => "$year-$month-10",
                'url' => "http://yahoo.com/"
        ),
  ));

  $headers = array(
        'Content-Type' => 'application/json'
  );

  $response = new Response($jsonData, 200, $headers);
  return $response;
}

Also I am assmuing that the twig file is rendered from different route, not CollegeStudentBundle_attendance_calendar route.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for ur reply. can u please elobrate ur answer. where exactly i need to do the change. the controller, or the view.
In studentCalenderAction method of the controller. The rendering return is just another new Response object after parsing the twig file. Not the Response object you created with the json encoded array.
return new JsonResponse(array('id' => 111,...)); can be helpful, without forgetting use Symfony\Component\HttpFoundation\JsonResponse;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.