haml - if-else with different identations

Posted by egarcia on Stack Overflow See other posts from Stack Overflow or by egarcia
Published on 2010-04-21T10:21:50Z Indexed on 2010/04/21 10:23 UTC
Read the original article Hit count: 272

Filed under:
|
|

Hi everyone,

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 on haml. This seems to require different levels of identation, 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 %tds 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>

I tried adding two more spaces to the else but then haml complained about improper identation.

What's the best way to do this?

Note: I'm not interested on rendering the calendar using unordered lists. Please don't suggest that.

© Stack Overflow or respective owner

Related posts about haml

Related posts about indentation