Fork me on Github

Silex Integration

Use CalendR inside your Silex application thanks to CalendRServiceProvider

Usage And registration

$app->register(new CalendR\Extension\Silex\Provider\CalendRServiceProvider(), array(
    'calendr.event.providers' => array(
        'my_awesome_event_provider' => new MyAwesomeEventProvider(),

To add CalendR support to Silex.

Now, you can access to the CalendR\Calendar factory by using $app['calendr'] in your Silex application.

Note : If you use Twig in your silex app, the CalendR Twig extension will be automatically registered.

Example display a month and retrieve events

Controller :


$app->get('/calendar/{year}/{month}', function($year, $month) use($app) {
    $calendRMonth = $app['calendr']->getMonth($year, $month);

    return $this->render('show_calendar.html.twig', array(
        'month'  => $calendRMonth,
        'events' => $app['calendr']->getEvents($calendRMonth->getExtendedMonth())

Note : Using $month->getExtendedMonth() is not mandatory, but it will retrieve events for displayed days that are out of month, and reduce call to the Event provider.

Template :

        {% for week in month %}
                {% for day in week %}
                        {{ day }}
                        {% set day_events = events.find(day) %}
                        {% if day_events|length > 0 %}
                                {% for event in day_events %}
                                    <li>{{ event }}</li>
                                {% endfor %}
                        {% endif %}
                {% endfor %}
        {% endfor %}

Note : Passing the events to the template is not mandatory, you can retrieve them directly in the template by using the twig function calendr_events(month).