Extension:Winter/Example 4
From MediaWiki.org
You may see this example live at wiki.swiftlytilting.com
Contents |
[edit] Winter 2.0
The following code is valid for Winter 2.1.0 and above
[edit] Code
{{#nocache}}
{{#
|| Initialize variables and functions
}}
{{#function | ShowDay ||
{{#if | {{#1 != 00}} ||
{{#if | {{#1 != {{CURRENTDAY}} }} ||
<span style=";;padding:.25em; margin:.5em;
margin-bottom:1.5em;margin-right:.5em; width:2em;">
||
<span style="background-color:#ccccff;;padding:.25em; margin:.5em; margin-bottom:1.5em;margin-right:.5em; width:2em;">
}}
||
<span style="background-color:#ffffff;color:#ffffff;padding:.25em; margin:.5em;
margin-bottom:1.5em;margin-right:.5em; width:2em;">
}}{{#1}}</span>
}}
{{#array | month | 31 | 28 | 31 | 30 | 31 | 30 | 31 | 31 | 30 | 31 | 30 | 31 }}
{{#days_in_month | @= | {{#month | {{#op {{CURRENTMONTH}} - 1 }} }} }}
{{#WeekStart | @= | {{#op | 8 - ( ( {{CURRENTDAY}} - {{CURRENTDOW}} ) mod 7 ) }} }}
{{#
|| Draw calendar
}}
<div style="border:1px solid black;width:22em;text-align:center;padding-bottom:.5em">{{#showday | {{CURRENTMONTHNAME}} {{CURRENTYEAR}} }}
{{#for | {{#i = 0}} || {{#i | < | {{#WeekStart}} }} || {{#i ++}} ||
{{#ShowDay| 00 }}
}}
{{#for | {{#day = 1}} || {{#day | <= | {{#days_in_month}} }} || {{#null}} ||
{{#for | {{#i | = | {{#WeekStart}} }} || {{#i < 7}} || {{#i ++}} {{#day ++}} ||
{{#ShowDay | {{#if | {{#day | > | {{#days_in_month}} }} || 00 || {{#if | {{#day < 10}} || 0}}{{#day}} }} }}
}}
<div style="clear:left"> </div>
{{#WeekStart @= 0}}
}}
</div>
[edit] Winter 1.5
The following code is valid for Winter 1.3 and above.
[edit] Code
{{#nocache}}
{{#setvar| MONTH_01 | 31 }}
{{#setvar| MONTH_02 | 28 }}
{{#setvar| MONTH_03 | 31 }}
{{#setvar| MONTH_04 | 30 }}
{{#setvar| MONTH_05 | 31 }}
{{#setvar| MONTH_06 | 30 }}
{{#setvar| MONTH_07 | 31 }}
{{#setvar| MONTH_08 | 31 }}
{{#setvar| MONTH_09 | 30 }}
{{#setvar| MONTH_10 | 31 }}
{{#setvar| MONTH_11 | 30 }}
{{#setvar| MONTH_12 | 31 }}
{{#setvar | days_in_month | {{#var|MONTH_{{CURRENTMONTH}} }} }}
{{#function | ShowDay || <span style="background-color:#cccccc;;padding:.25em;margin:.5em; margin-bottom:1.5em;margin-right:.5em; width:2em;">{{#1}}</span>
}}
{{#setvar| WeekStart | {{#op | 8 | - | {{#op | {{#op | {{CURRENTDAY}} | - | {{CURRENTDOW}} }} | mod | 7 }} }} }}
<div style="border:1px solid black;width:22em;text-align:center;padding-bottom:.5em">
<span style="background-color:#cccccc; padding:.25em; margin:.5em; margin-bottom:1.5em; margin-right:.5em; width:2em;">{{CURRENTMONTHNAME}} {{CURRENTYEAR}}</span>
{{#for | {{#setvar|i|0}} || {{#i| < |{{#WeekStart}} }} || {{#i|++}}
|| {{#ShowDay| 00 }}
}}{{#for | {{#setvar | day | 1}} || {{#day | <= | {{#days_in_month}} }} || {{#null}}
|| {{#for | {{#setvar|i|{{#WeekStart}}}} || {{#i | < | 7}} || {{#i|++}} {{#day|++}}
|| {{#ShowDay | {{#if|{{#day|<|10}}|0}}{{#if|{{#day | > | {{#days_in_month}}}}| 00 |{{#day}}}} }} }}
<div style="clear:left"> </div>
{{#setvar|WeekStart|0}}}}
</div>
[edit] Explanation
The above code will produce a simple calendar for the current month. Lets take a look at each section of code to get a better idea of whats going on.
{{#nocache}}
Disables the cache so that the calendar is drawn each time. Otherwise the calendar page would only show the current month when it was cached.
{{#setvar| MONTH_1 | 31 }}
{{#setvar| MONTH_2 | 28 }}
{{#setvar| MONTH_3 | 31 }}
{{#setvar| MONTH_4 | 30 }}
{{#setvar| MONTH_5 | 31 }}
{{#setvar| MONTH_6 | 30 }}
{{#setvar| MONTH_7 | 31 }}
{{#setvar| MONTH_8 | 31 }}
{{#setvar| MONTH_9 | 30 }}
{{#setvar| MONTH_10 | 31 }}
{{#setvar| MONTH_11 | 30 }}
{{#setvar| MONTH_12 | 31 }}
{{#setvar | days_in_month | {{#var|MONTH_{{CURRENTMONTH}} }} }}
The above setvar commands demonstrate how to create rudimentary arrays. Wiki variables like CURRENTMONTH are replaced before Winter ever sees them, so MONTH_{{CURRENTMONTH}} appears as MONTH_11 to Winter.
{{#function | ShowDay || <span style="background-color:#cccccc;;padding:.25em;margin:.5em; margin-bottom:1.5em;margin-right:.5em; width:2em;">{{#1}}</span>
}}
Define a function to display the date box. this is to make the loop code easier to read
{{#setvar| WeekStart | {{#op | 8 | - | {{#op | {{#op | {{CURRENTDAY}} | - | {{CURRENTDOW}} }} | mod | 7 }} }} }}
Sets WeekStart to ( 8 - ((CURRENTDAY - CURRENTDOW) mod 7)) which will away give you the day of the week that the month started on in the form of 0 - 6 just like CURRENTDOW.
<div style="border:1px solid black;width:22em;text-align:center;padding-bottom:.5em">
<span style="background-color:#cccccc; padding:.25em; margin:.5em; margin-bottom:1.5em; margin-right:.5em; width:2em;">{{CURRENTMONTHNAME}} {{CURRENTYEAR}}</span>
Draw the calendar header in html
{{#for | {{#setvar|i|0}} || {{#i| < |{{#WeekStart}} }} || {{#i|++}}
|| {{#ShowDay| 00 }}
Adds the blank days before the start day (note: the closing }} is in the code below)
}}{{#for | {{#setvar | day | 1}} || {{#day | <= | {{#days_in_month}} }} || {{#null}}
|| {{#for | {{#setvar|i|{{#WeekStart}}}} || {{#i | < | 7}} || {{#i|++}} {{#day|++}}
|| {{#ShowDay | {{#if|{{#day|<|10}}|0}}{{#if|{{#day | > | {{#days_in_month}}}}| 00 |{{#day}}}} }} }}
<div style="clear:left"> </div>
{{#setvar|WeekStart|0}}}}
This nested for loop draws out each box, first by day then by week. It is equivalent to the following PHP code:
for ($day = 1; $day <= $days_in_month;)
{ for ($i = $WeekStart; $i < 7; $i++, $day++)
{ ShowDay( (($day < 10) ? : "0" : "") . (($day > $days_in_month) : 00 : $day)
}
echo '<div "style=clear:left"></div>';
$WeekStart = 0;
}
Pleawe note this code:
ShowDay( (($day < 10) ? : "0" : "") . (($day > $days_in_month) : 00 : $day)
displays a leading 0 if the date is less than 10, or 00 if the date is greater than the days in the month.
</div>
Closes box