Help:Templates
Complex MediaWiki templates can be a difficult thing to grasp. Editors with a programming background will find few bits of common ground with their language of choice: there are no data types, no loops, no conditionals in the traditional sense of the word. But sometimes these things can be simulated.
[edit] Conditionals
Sometimes, you want to substitute default data if a template parameter is not specified. For example, imagine {{useremail}} with the following contents:
[[User:{{{1}}}|{{{1}}}]] {{ifthen|=<[mailto:{{{2}}}]>|{{{2}}}=(no mail)}}
{{ifthen}} is a very simple generic template:
{{{}}}
It may not be immediately obvious, but {{{}}} is a named parameter. You may have seen template calls like {{supertemp|name=My Template}}. Inside the template, "name" parameter is referenced by {{{name}}}. Our parameter is the same, with one important difference: our parameter name in this case is an empty string.
[edit] In Practice
A standard call to {{Template:useremail}} would look like this:
| Function call: | {{useremail|AdamBackstrom|adam@sixohthree.com}} |
| Template substitution: | [[User:AdamBackstrom|AdamBackstrom]] {{ifthen|=<[mailto:adam@sixohthree.com]>|adam@sixohthree.com=(no mail)}} |
| Final output: | AdamBackstrom <[1]> |
However, the template also allows for cases where the second parameter is included, but empty:
| Function call: | {{useremail|AdamBackstrom|}} |
| Template substitution: | [[User:AdamBackstrom|AdamBackstrom]] {{ifthen|=<[mailto:]>|=(no mail)}} |
| Final output: | AdamBackstrom (no mail) |
Where the first template call defined two variables, {{{}}} and {{{adam@sixohthree.com}}}, the second defined {{{}}} twice in a row. The last declaration takes precedence, so the displayed value is "(no mail)".