[Nitro] Questions on Element, Control and rendering

Joshua Hoke nitro at tap.homeip.net
Sat Jan 21 15:51:23 EST 2006


(Resent with the correct From address.)

On Wed, Jan 18, 2006 at 03:50:54PM +0200, George Moschovitis wrote:
> > As an example, I want to code a generic calendar that I can use
> > everywhere I need/want.
> 
> funny, i was working on this too ;-)


This is what I use Nitro for (to generate a calendar of events.) I don't
know how much you have worked out already, but attached is some code
that might be helpful to generate the calendar. You should be able to
use it like this:

make_month_table(2006, 1) { |date|
    response = "#{date.mday}"
    # You could also add events or whatever content you want to go
    # in the cell for this day.

    return response
}

I guess it's up to you how to integrate it with what you're doing.

Joshua Hoke
-------------- next part --------------
require "date"
require "time"

def make_month_table(year, month)
    html = "<table class=\"cal\">"
    html += "<thead>\n"
    html += "<tr class=\"weekdays\"><td>"
    html += Date::DAYNAMES.join("</td><td>")
    html += "</td></tr>\n"
    html += "</thead>\n"
    start_date = Date::civil(year, month, 1)
    end_date = (start_date >> 1) - 1

    padcount = start_date.wday
    html += "\n<tr class=\"calrow\">\n" if padcount > 0
    while padcount > 0
        html += "  <td></td>\n"
        padcount -= 1
    end

    start_date.step(end_date, 1) do |date|
        html += "<tr class=\"calrow\">\n" if date.wday == 0
        html += "  <td>\n"
        html += yield date
        html += "</td>\n"
        html += "</tr>\n" if date.wday == 6
    end

    if end_date.wday != 6
      (end_date.wday+1).upto(6) { |x| html += "<td></td>\n" }
      html += "\n</tr>"
    end
    html += "\n</table>\n"

    return html
end


More information about the Nitro-general mailing list