Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 739865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:26:59+00:00 2026-05-14T08:26:59+00:00

I’m trying to render a calendar with Rails and Haml . The dates used

  • 0

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
  -@dates.each 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>
      <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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:26:59+00:00Added an answer on May 14, 2026 at 8:26 am

    Evgeny’s comment put me on the right track.

    Here’s a solution that works, using rails’ in_groups_of(link is dead right now):

    %table(cellspacing="0" cellpadding="0")
      %tr
        %th= t('date.day_names')[0]
        %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]
    
      - @dates.to_a.in_groups_of(7) do |week|
        %tr
          - week.each do |day|
            %td=day.to_s
    

    Notice that I had to convert the Range into an Array – ranges don’t seem to implement in_groups_of.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.