On 11/16/06, Michael Steinfeld <[EMAIL PROTECTED]> wrote:
> I am a bit list how I would do this correctly using kid and the correct MVC
> approach. I appreciate any help.
In general, when I run into situations like this, I find it's clearer
to do as much logic and calculation as you can in the controller, and
keep as much text as possible in the template. So I'd probably do
something like this:
@expose(template="tgcal.templates.events")
def calendar(self, year='2006', month='1', day=None):
year, month = int(year), int(month)
import calendar
calendar.setfirstweekday(calendar.SUNDAY)
daysevents = model.Event.selectBy(year=year,month=month)
event_count = dict((i,0) for i in range(0, num_days+1))
for event in daysevents:
event_count[event.day] += 1
monthcal = [[(day, event_count[day]) for day in week]
for week in calendar.monthcalendar(year,month)]
[rest of logic, then return the dict]
Then the template becomes a bit simpler (note that I changed the name
of "days" and "day" in the iterations, as the latter was overshadowing
the "day" passed in via the controller's return):
<tr py:for="cal_week in monthcal">
<td py:for="cal_day, num_events in cal_week">
<br py:if="not cal_day" />
<span py:if="cal_day" py:strip="">
<a class="showday" href="/calendar/${year}/${month}/${cal_day}">
${cal_day}
</a><span py:if="cal_day" py:strip="">
<br/>
<a class="event" href="/post/${year}/${month}/${day}">[add]</a>
<br/>
<span py:if="not num_events" py:strip="">No Events Today</span>
<span py:if="1 == num_events" py:strip="">1 Event Today</span>
<span py:if="1 < num_events" py:strip="">${num_events} Events
Today</span>
</span>
</td>
</tr>
I find the repeated <span> more readable than an and/or hack embedded
in the text, plus it's more localizable.
--
Tim Lesher <[EMAIL PROTECTED]>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---