Simple Calendar
Here is a simple calendar that utilizes the Python calendar module and prints out an HTML table to display the output.
All of the code is in the template, with the intention that the "calendar" macro could be included in any page of the module without needing to initialize its values. The cal.py page module is just a dummy module to run the template for this example.
Purpose
Demonstrates:
- nested al-for tags. Outer al-for loops over the weeks. Inner al-for loops over the days of the week.
- al-exec
- al-if / al-else
- al-macro
Source Code
cal.py
{{{def page_process(ctx):
- pass
def page_display(ctx):
- ctx.run_template("cal.html")
}}}
cal.html
{{{<al-macro name="calendar"> <al-exec expr="import calendar import time now = time.localtime() thisyear, thismonth, thisdate = (now[0], now[1], now[2]) calendar.setfirstweekday(6) monthcal = calendar.monthcalendar(thisyear, thismonth) daynames = calendar.weekheader(2).split() calheader = time.strftime('%B %Y', now) " />
<table width="49px" bgcolor="#000000" cellspacing="1" cellpadding="4"> <tr bgcolor="#999999">
<td colspan="7"><font color="#FFFFFF"><b><al-value expr="calheader" /></b></font></td>
</tr>
<tr bgcolor="#CCCCCC" > <al-for iter="dayname" expr="daynames">
<td><al-value expr="dayname.value()" /></td>
</al-for> </tr>
<al-for iter="weekrow" expr="monthcal"> <tr bgcolor="#FFFFFF">
<al-for iter="daynum" expr="weekrow.value()"> <td>
<al-if expr="daynum.value()">
<al-value expr="str(daynum.value())" />
<al-else>
</al-if>
</td> </al-for>
</tr> </al-for> </al-macro>
<html> <head><title>Calendar</title></head> <body> <al-expand name="calendar" /> </body> </html> }}}