Template:Featured image/doc/underthehood

Under the hood
What follows is a detailed, line by line explanation of how this template works, for those who really want to know. This template uses DynamicPageList (DPL) which is fully documented here. Below is the essential part of the Featured image template: 1.
 * 1) The first line declares we are using DPL in parser function mode (like ).
 * 2) This tells DPL to limit its search to only files in the "Template" namespace, meaning all files that begin with.
 * 3) *The date files containing the featured images are all in the Template namespace.
 * 4) For this template, we are searching for one particular file, so we might as well search for it by title. If the template is called with an argument (like ), then   is replaced by 22 April. If called without an argument,   is undefined and the default value is substituted instead. Using 22 April for example, the full title of the page we are searching for would be "Template:Featured image/22 April".
 * 5) *The default value is the concatenation of the two Magic Words  and   separated by a space. The last time this page (the one you are reading right now) was cached, the default value was 28  GMT.
 * 6) Assuming the page was found, this tells what part of the page to fetch and present as the output of this template. We want all Format featured image templates, rendered just as if they were on the page in place of Featured image.
 * 7) *If the page we found did not contain a Format featured image template, then DPL would give up at this point and call Format featured image.default and return those results, whatever they might be.
 * 8) If we find one or more Format featured image templates on the page, then they have to match certain criteria, given by this regular expression, or they will be skipped. In English, the regular expression is looking for possible whitespace, then the word "usethis", followed by maybe more whitespace, an equals sign, yet more optional whitespace, then either the word "all" or the value of the AM/PM field in the current timestamp. If a match is found, then the Format featured image template is sent to the output and we move on to the next one, if any. Here's a slightly more technical description of the regular expression:
 * 9) *The regexp starts and ends with a forward slash, " / ". The "s" at the end specifies that the parser should consider newline characters as whitespace (which means, for example, that "usethis" and "=" don't have to be on the same line in the "22 April" file. The "i" at the end specifies that all string comparisons should ignore case (which means you can write "useThis = PM" if you want).
 * 10) *"\s" is shorthand for "whitespace" and the " * " means "zero or more of these". So "\s*" matches any amount of whitespace (including newlines because of the trailing "s") or no spaces at all (so you can write "usethis=AM" if you want).
 * 11) *Next it tries to match the string "usethis" (ignoring case due to the trailing "i").
 * 12) *Match "\s*" for zero or more whitespace.
 * 13) *Match an equals sign " = "
 * 14) *Match "\s*" for zero or more whitespace.
 * 15) *The left parenthesis, " ( " starts a subpattern. We can match any of the things inside the parentheses. They are separated by the broken pipe symbol, " ¦ " which performs a logical OR function. Normally, regular expressions use the pipe symbol, " | " here, but since templates use the pipe to separate parameters, it would confuse the parser, so we use the broken pipe instead.
 * 16) *The first optional subpattern match is the word "all".
 * 17) *The second optional subpattern is the current AM/PM indicator as given by . So whenever the Featured image template is called,  will be replaced by either "am" or "pm" depending on whether it is before noon or after noon, GMT. The last time this doc page was cached, it was .
 * 18) *The right parenthesis closes off the subpattern.
 * 19) *The last forward slash closes off the regexp, leaving the "si" which we discussed earlier. We're done!
 * 20) This line tells DPL what to do if the file matching the title does not exist&mdash;we call Format featured image.default with either the value of the parameter  ("22 April" in our example), or the current day month pair, if we didn't specify a date. The .default template will compute the nearest day that is a multiple of 7, plus 1, and try again. Right now, it computes .
 * 21) This tells DPL to ignore any cached results (which might be stale by a few hours) and return fresh results.
 * 22) And finally, this command tells DPL to remove any categories from the included code and remove references to any images before sending the output to the page that called this template. Otherwise, the calling page would be put in whatever categories had been assigned to the Format featured image, and the "What links here" report for any images included would show that the calling page linked them, which is not true.