Help:Advanced templates

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] Advanced templates

This page covers advanced template techniques. You should already be familiar with basic template techniques before reading this page.

[edit] Using variables and parameters to effect what happens

You can use variables like {{CURRENTDAY}} or {{NAMESPACE}} to effect what templates do. {{ {{NAMESPACE}} }} for instance changes what template is included depending on the namespace. Template:Help is used when in the Help namespace, Template:Wikibooks is used when in the Wikibooks namespace, and so on.

Similarly you can use parameters passed to templates to effect what happens. Another way to do the above example is using {{ {{{1}}} }} and than passing {{NAMESPACE}} as the first parameter to the template.

Examples
if Template:Example contained: if you did: results in:
{{ {{NAMESPACE}} }} {{example}} {{Help}} (depends on namespace)
{{ {{{1}}} }} {{example|{{NAMESPACE}}}} {{Help}} (depends on namespace)
{{ {{CURRENTDAYNAME}}'s chart }} {{example}} {{Thursday's chart}} (depends on what the current day is)
{{water|color={{{1}}}}} {{example|blue}} {{water|color=blue}}
{{water|{{{1}}}=blue}} {{example|color}} {{water|color=blue}}
{{water|{{{1}}}={{{2}}}}} {{example|color|blue}} {{water|color=blue}}
{{table|{{ {{{1}}} }}}} {{example|poem}} {{table|{{poem}}}}
{{table|body={{ {{{1}}} }}}} {{example|poem}} {{table|body={{poem}}}}

[edit] Using one template to effect another

Like with variables and parameters, you can also use one template to effect what another template does.

If Template:A consists of the text "example" then {{ {{a}} }} would end up including Template:Example's content. This is because {{ {{a}} }} expands to {{example}} first. You could even use Template:Example's content as the name of yet another template to expand, and so on.