I'm trying to render a calendar with Rails and Haml.
The dates used come from a variable called @dates
. It is a Date
range that contains the first and last days to be presented on the calendar. The first day is always Sunday and the last one is always Monday.
I'm planning to render a typical calendar, with one column per weekday (Sunday is going to be the first day of the week) using an HTML table.
So, I need to put a %tr
followed by a %td
on Sundays, but the rest of the days I just need a %td
.
I'm having trouble modelling that in Haml. This seems to require different levels of indentation, and that's something it doesn't like. Here's my failed attempt:
%table
%tr
%th= t('date.day_names')[0] # Sunday
%th= t('date.day_names')[1]
%th= t('date.day_names')[2]
%th= t('date.day_names')[3]
%th= t('date.day_names')[4]
%th= t('date.day_names')[5]
%th= t('date.day_names')[6] # Monday
[email protected] do |date|
- if(date.wday == 0) # if date is sunday
%tr
%td=date.to_s
- else
%td=date.to_s
This doesn't work the way I want. The %td
s for the non-Sunday days appear outside of the %tr
:
<tr>
<td>2010-04-24</td>
</tr>
<td>2010-04-25</td>
<td>2010-04-26</td>
<td>2010-04-27</td>
<td>2010-04-28</td>
<td>2010-04-29</td>
<td>2010-04-30</td>
<tr>
<td>2010-05-01</td>
</tr>
<td>2010-05-02</td>
<td>2010-05-03</td>
...
I tried adding two more spaces to the else
but then Haml complained about improper indentation.
What's the best way to do this?
Note: I'm not interested on rendering the calendar using unordered lists. Please consider using a table as one of the problem's constraints.