A simple helper for creating an XHTML and CSS calendar.
The “calendar” and “day_months” methods will be automatically available to your view templates.
./script/plugin install calendar_helper
<%= calendar :month => 10, :year => 2007 %>
The calendar helper implements something similar to blocks in Ruby by using PHP variable references.
This example will add the class specialDay to the days found in the array $list_of_special_days
<? while($calendar_helper->month_days(array('month' => 6, 'year' => 2010), $d)) : if(in_array($d->day, $list_of_special_days)){ $d->cell_attributes = array('class' => 'specialDay'); } endwhile; ?>
This example will replace the inner content of the cell with the content you output in the while loop, when the day is found in the $list_of_special_days array. This is really convenient for generating links and Ajax for a given day in the calendar.
<? while($calendar_helper->month_days(array('month' => 9, 'year' => 2006), $d)) : if(in_array($d->day, $list_of_special_days)) : ?> <%= link_to d.day, :controller => 'events', :day => d.day %> <? endif; endwhile; ?>
./script/generate calendar_styles
CSS will be copied to subdirectories of public/stylesheets/calendar.
stylesheets/calendar/default/style.css
<%= calendar :year => 2007, :month => 10 %>
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 30 | 1 | 2 | 3 | 4 | 5 | 6 |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | 1 | 2 | 3 |
stylesheets/calendar/blue/style.css
<%= calendar :year => 2007, :month => 10 %>
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 30 | 1 | 2 | 3 | 4 | 5 | 6 |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | 1 | 2 | 3 |
stylesheets/calendar/blue/grey.css
Generated with
<%= calendar :year => 2007, :month => 10, :abbrev => 3 %>
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
| 30 | 1 | 2 | 3 | 4 | 5 | 6 |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | 1 | 2 | 3 |
stylesheets/calendar/blue/style.css
Generated using this Sintags snippet
<%= calendar({:year => 2007, :month => 10, :first_day_of_week => 1, :day_names => {'D','L','M','X','J','V','S'} }) %>
| L | M | X | J | V | S | D |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 | 1 | 2 | 3 | 4 |