BIONICLEsector01:Help/Templates

From BIONICLEsector01

What are templates?

Templates are wiki pages meant to be embedded, or transcluded, within other pages.

Templates often contain content that is shared across many wiki pages so that the information can be stored in one central place instead of being copy/pasted everywhere it is used.

For example, Template:CN contains a flag for marking uncited information. Articles on BS01 transclude Template:CN to point out uncited information to readers. Template:CN is transcluded at the end of this sentence.[citation needed] You can also provide extra information to templates that changes how they behave. At the end of this sentence, we have fed Template:CN additional information about a possible source so it can mention that source for readers.[citation needed: BIONICLE: Encyclopedia?]

Why use a template instead of typing "citation needed" every time you need to? Using a template is simpler and makes every use of "citation needed" look the same. Any edits that users make to Template:CN will percolate to this page, and to every other page that uses the template. For example, if someone edits Template:CN to have a yellow background, then both "citation needed" flags above would become yellow, even though no edits have been made to this page!

How can users place templates in pages?

You can transclude most wiki pages into other pages. The syntax for transcluding a wiki page depends on the page's namespace.

  • Main namespace: To transclude the page at ARTICLE_TITLE, type {{:ARTICLE_TITLE}}. (For example, to transclude Vakama, type {{:Vakama}}.)
  • All other namespaces: To transclude the page at NAMESPACE_TITLE:PAGE_TITLE, type {{NAMESPACE_TITLE:PAGE_TITLE}}. (For example, to transclude Set:6934, type {{Set:6934}}.)
  • Template namespace: Per the previous bullet point, to transclude the page at Template:TEMPLATE_TITLE, you can type {{Template:TEMPLATE_TITLE}}. That said, since most templates are in the Template namespace, BS01's wiki software provides a shortcut to transclude those pages: to transclude the page at Template:TEMPLATE_TITLE, just type {{TEMPLATE_TITLE}}. (For example, to transclude Template:CN, you can type either {{Template:CN}} or {{CN}}.) In practice, users usually write the shorter version.

Transcluding parts of pages

Sometimes, you might want to transclude only part of a template's wiki page. To do this, you can add special tags to the template page.

Tag name Description Wikitext on template page Result on template page Result when transcluded
noinclude Show this text on the template page, but hide it when transcluded
<noinclude>Example text</noinclude>
Example text
includeonly Hide this text on the template page, but show it when transcluded
<includeonly>Example text</includeonly>
Example text
onlyinclude Show this text on the template page, and show it when transcluded, but hide all other text when transcluded; can override other tags
<onlyinclude>Example text</onlyinclude>
Example text Example text

How can users customize templates?

Parameters

Parameters are pieces of information that users can pass into a template to customize it. When the template is transcluded, users can specify values for some or all of the template's parameters. By passing in different values for a template's parameters, users can change how the template behaves.

Specifically, when users create templates, they write placeholder text at each place where a parameter should go. When the template is transcluded, each piece of placeholder text is replaced with the value of its corresponding parameter.

If parameters and placeholders seem abstract right now, don't worry! The sections below contain plenty of concrete examples.

Named parameters

To add a named parameter called PARAMETER_NAME, write the placeholder text {{{PARAMETER_NAME}}} inside of the template's wiki page where you want the parameter to appear. For example, imagine that the wiki page called Template:FavoriteToa contains the following wikitext.

''My favorite Toa is named {{{toa_name}}}. They are a Toa of {{{toa_element}}}.''

The template has two parameters called toa_name and toa_element. When you transclude that template, you can fill in the values of its parameters. Then the placeholder text {{{toa_name}}} is replaced with the value that you give the toa_name parameter. Similarly, the placeholder text {{{toa_element}}} is replaced with the value that you give the toa_element parameter.

To transclude the template while filling in its parameters, you would use the snippet below.

{{FavoriteToa|toa_name=Voriki|toa_element=Energy}}

The snippet above would produce the following text.

My favorite Toa is named Voriki. They are a Toa of Energy.

You could just as easily reverse the order of the parameters and get the same result. An example is shown below; note that the value for toa_name is provided second, even though {{{toa_name}}} is the first placeholder text that appears in Template:FavoriteToa.

{{FavoriteToa|toa_element=Energy|toa_name=Voriki}}

Note that when transcluding large templates with many parameters, it's often easier to place each parameter on its own line, as shown below.

{{FavoriteToa
|toa_name=Voriki
|toa_element=Energy
}}

Anonymous parameters

Template parameters do not need names. Unnamed parameters are called anonymous parameters and are made by giving the parameters numbers instead of names. For example, imagine that the wiki page called Template:FavoriteToa contains the following wikitext.

'''My favorite Toa is named {{{1}}}. They are a Toa of {{{2}}}.'''

The template uses the anonymous parameters 1 and 2. When you transclude the template, you can specify values for parameters 1 and 2. To transclude the template while filling in its parameters, you would use the snippet below.

{{FavoriteToa|Voriki|Energy}}

The snippet above would produce the following text.

My favorite Toa is named Voriki. They are a Toa of Energy.

Since the parameters are anonymous (numbered), you do not need to specify their names. The first value is automatically assigned to parameter 1, at which point the placeholder text {{{1}}} is replaced with that parameter's value. Similarly, the second value is automatically assigned to parameter 2, at which point the placeholder text {{{2}}} is replaced with that parameter's value.

You could just as easily treat these anonymous parameters as if were named parameters with the names 1 and 2. In that case, you could use the code snippet below to produce the same result as before.

{{FavoriteToa|1=Voriki|2=Energy}}

Default values for placeholder text

In the earlier examples, if you did not specify a value for a parameter, then its placeholder text would still appear. For example, imagine that the wiki page called Template:FavoriteToa contains the following wikitext.

''My favorite Toa is named {{{toa_name}}}. They are a Toa of {{{toa_element}}}.''

The snippet below transcludes the template and fills in its toa_name parameter, but not its toa_element parameter.

{{FavoriteToa|toa_name=Jaller}}

The snippet above would produce the following text.

My favorite Toa is named Jaller. They are a Toa of {{{toa_element}}}.

When the template was transcluded, no value was provided for toa_element, so the placeholder text {{{toa_element}}} could not be replaced with anything.

To avoid this behavior, you can give each piece of placeholder text a default value. The placeholder text {{{PARAMETER_NAME|DEFAULT_VALUE}}} means the following: "If the parameter called PARAMETER_NAME is given a value, then replace the placeholder text with that value. Otherwise, replace the placeholder text with DEFAULT_VALUE."

For example, imagine that the wikitext below is in the wikipage called Template:FavoriteToa.

''My favorite Toa is named {{{toa_name|Lhikan}}}. They are a Toa of {{{toa_element|Fire}}}.''

The template contains the placeholder text {{{toa_name|Lhikan}}}. If the toa_name parameter is given a value, then the text {{{toa_name|Lhikan}}} will be replaced with that value. Otherwise, the text {{{toa_name|Lhikan}}} will be replaced with Lhikan.

The placeholder text {{{toa_element|Fire}}} behaves similarly. If the parameter toa_element is given a value, then {{{toa_element|Fire}}} will be replaced with that value. Otherwise, {{{toa_element|Fire}}} will be replaced with Fire.

To see how placeholder defaults work in action, consider the snippet below.

{{FavoriteToa}}

That snippet would produce the following text.

My favorite Toa is named Lhikan. They are a Toa of Fire.

Why would this happen? When we transcluded the template, we did not specify a value for either toa_name or toa_element. Therefore, the placeholder text {{{toa_name|Lhikan}}} was replaced with Lhikan, and similarly, the placeholder text {{{toa_element|Fire}}} was replaced with Fire.

Now consider the example below.

{{FavoriteToa|toa_name=Jaller}}

That snippet would produce the following text.

My favorite Toa is named Jaller. They are a Toa of Fire.

Why would this happen? When we transcluded the template, we specified a value for for toa_name, so {{{toa_name|Lhikan}}} is replaced with that value (Jaller). Meanwhile, no value was provided for toa_element, so {{{toa_element|Fire}}} was replaced with Fire.

To make placeholder text completely disappear if no value is given, make its default value empty. For example, imagine that the wiki page called Template:FavoriteQuote contains the following wikitext.

''My favorite quote is, "{{{quote_text|}}}"''

Notice that {{{quote_text|}}} should default to the value between | and }}}. No text is between those two things--the default value is completely empty--so the text should disappear if quote_text is not provided.

If Kopeke used this template, as shown below, he would probably not provide a value for the quote_text parameter.

{{FavoriteQuote}}

The snippet above would produce the following text.

My favorite quote is, ""

Variables

Variables are special, pre-defined pieces of wikitext that act a lot like templates, except they are not stored in any wiki page. Instead, BS01's wiki software provides variables by default. Adding variables to a template can be a useful way to customize it.

A full list of variables is available at Help:Magic words#Variables on MediaWiki.org, the website for the wiki software that BS01 uses. In this article, we will only work with a couple interesting variables to show how variables might be useful.

  • {{FULLPAGENAME}}: Outputs the title of this wiki page. For this page, the output is BIONICLEsector01:Help/Templates.
  • {{REVISIONUSER}}: Outputs the username of the user who saved this page's most recent edit. For this page, the output is Morris the Mata Nui Cow.

Variables are useful inside of templates because when a template is transcluded, the variables inside that template take on different values based on where they are transcluded. For example, imagine that the wiki page called Template:ArticleWelcome contains the following.

''Welcome to '''{{FULLPAGENAME}}'''! The user who most recently edited this article is called '''{{REVISIONUSER}}'''.''

If we transcluded that template on this page by typing {{ArticleWelcome}}, then the following text would appear.

Welcome to BIONICLEsector01:Help/Templates! The user who most recently edited this article is called Morris the Mata Nui Cow.

Sure enough, if you check the edit history of this page, you can verify for yourself that Morris the Mata Nui Cow is the user who most recently edited this article!

Logic

Templates can perform logic based on the values of their parameters and variables. This logic lets the templates act in complex ways. Templates perform this logic using tools called parser functions.

A full list of parser functions is available at Help:Extension:ParserFunctions on MediaWiki.org, the website for the wiki software that BS01 uses. In this article, we will only work with only a few interesting parser functions to show how they might be useful.

#if

The #if parser function takes in a piece of text (also called a string) as input. It outputs one of two strings, depending on whether or not the input text is empty. (Text is empty if it only contains whitespace such as spaces, line breaks, and so forth.)

Syntax

The #if parser function's syntax is below.

{{#if: INPUT_STRING | OUTPUT_STRING_IF_INPUT_STRING_IS_NOT_EMPTY | OUTPUT_STRING_IF_INPUT_STRING_IS_EMPTY}}
With static text

Here are a few basic examples of the #if parser function in action.

  • {{#if: blah | a | b}} outputs a. Since the input string blah is not empty, the parser function outputted its first output string, a.
  • {{#if: | a | b}} outputs b. Since the input string is empty, the parser function outputted its second output string, b.
With placeholder text

In templates, the #if parser function is useful because it can adjust a template's behavior depending on whether or not a parameter has a value.

As discussed in the "Default values for placeholder text" section above, the placeholder text {{{PARAMETER_NAME|}}} behaves as follows: if a value is specified for the parameter called PARAMETER_NAME, then {{{PARAMETER_NAME|}}} gets replaced with that value, and otherwise {{{PARAMETER_NAME|}}} becomes empty. By using this placeholder value with an #if parser function, we can make a template behave differently depending on whether or not PARAMETER_NAME has a value!

For example, imagine that the wiki page called Template:FavoriteElement contains the following wikitext.

''My favorite element is {{{element_name}}}. Why? {{#if: {{{element_reason|}}}|Because {{{element_reason}}}|Just because}}!''

Imagine transcluding the template with the snippet below.

{{FavoriteElement|element_name=Ice}}

The template would produce the following text.

My favorite element is Ice. Why? Just because!

Why did this happen? The template's #if parser function checked whether {{{element_reason|}}} was empty. Since no value was provided for element_reason, the text {{{element_reason|}}} was empty. Since that text was empty, the #if parser function outputted its second output string, Just because.

Now imagine that we did specify a value for element_reason, as shown below.

{{FavoriteElement|element_name=Ice|element_reason=I'm cool}}

In that case, the template would produce the following text.

My favorite element is Ice. Why? Because I'm cool!

Why did this happen? The template's #if parser function checked whether {{{element_reason|}}} was empty. Since element_reason was given a value of I'm cool, the placeholder text {{{element_reason|}}} became I'm cool and was therefore nonempty. Since that text was nonempty, the #if parser function outputted its first output string. That first output string was Because {{{element_reason}}}, but of course, since element_reason had the value of I'm cool, that string was modified to Because I'm cool.

#ifeq

The #ifeq parser function takes in two pieces of text (also known as "strings") as input. It outputs one of two strings, depending on whether or not two pieces of input text are equal.

Syntax

The #ifeq parser function's syntax is below.

{{#ifeq: INPUT_STRING_1 | INPUT_STRING_2 | OUTPUT_STRING_IF_INPUT_STRINGS_ARE_EQUAL | OUTPUT_STRING_IF_INPUT_STRINGS_ARE_NOT_EQUAL}}
With static text

Here are a few basic examples of the #ifeq parser function in action.

  • {{#ifeq: x | x | equal | not equal}} outputs equal. Since the two input strings x and x are equal, the parser function outputted its first output string, equal.
  • {{#ifeq: x | y | equal | not equal}} outputs not equal. Since the two input strings x and y are not equal, the parser function outputted its second output string, not equal.
With placeholder text

In templates, the #ifeq parser function is useful because it can adjust a template's behavior depending on the parameter's value.

For example, imagine that the wiki page called Template:GuessingGame contains the following wikitext.

''You guessed that Vorox's favorite food is {{{guess}}}. You are {{#ifeq: {{{guess}}}|Thornax Stew|correct|incorrect}}!''

Imagine transcluding the template with the snippet below.

{{GuessingGame|Seaweed}}

The template would produce the following text.

You guessed that Vorox's favorite food is Seaweed. You are incorrect!

The template's #ifeq parser function checked whether the placeholder text {{{guess}}} was equal to Thornax Stew. Since the parameter guess was set to Seaweed, the placeholder text {{{guess}}} was replaced with Seaweed, which was not equal to Thornax Stew. As a result, the parser function outputted its second output string, incorrect.

Imagine transcluding the template with the snippet below.

{{GuessingGame|Thornax Stew}}

In that case, the template would produce the following text.

You guessed that Vorox's favorite food is Thornax Stew. You are correct!

The template's #ifeq parser function checked whether the placeholder text {{{guess}}} was equal to Thornax Stew. Since the parameter guess was set to Thornax Stew, the placeholder text {{{guess}}} was replaced with Thornax Stew, which was indeed equal to Thornax Stew. As a result, the parser function outputted its first output string, correct.

#switch

The #switch parser function takes in a piece of text (also known as a "string") as input. It outputs one of several strings, depending on what its input string is equal to. (It's similar to #ifeq, but instead of comparing one string with another, it checks one string against multiple others.)

Syntax

The #switch parser function's basic syntax is below.

{{#switch: INPUT_STRING
|COMPARISON_STRING_1 = OUTPUT_STRING_1
|COMPARISON_STRING_2 = OUTPUT_STRING_2
|COMPARISON_STRING_3 = OUTPUT_STRING_3
|COMPARISON_STRING_4 = OUTPUT_STRING_4
|#default = DEFAULT_OUTPUT_STRING
}}

If INPUT_STRING equals COMPARISON_STRING_1, then the parser function will output OUTPUT_STRING_1. Similarly, if INPUT_STRING equals COMPARISON_STRING_2, then the parser function will output OUTPUT_STRING_2, etc. If INPUT_STRING does not equal any of the COMPARISON_STRINGs, then the parser function will output DEFAULT_OUTPUT_STRING.

There are other ways to write this parser function. If two COMPARISON_STRINGs should produce the same OUTPUT_STRING, then you can group the COMPARISON_STRINGs together as shown below.

{{#switch: INPUT_STRING
|COMPARISON_STRING_1 = OUTPUT_STRING_1
|COMPARISON_STRING_2 = OUTPUT_STRING_2
|COMPARISON_STRING_3 = OUTPUT_STRING_3
|COMPARISON_STRING_4
|COMPARISON_STRING_4_ALTERNATE = OUTPUT_STRING_4
|#default = DEFAULT_OUTPUT_STRING
}}

In the example above, the parser function outputs OUTPUT_STRING_4 if INPUT_STRING equals either COMPARISON_STRING_4 or COMPARISON_STRING_4_ALTERNATE.

Also, if you want, you can remove the text #default = , as shown below.

{{#switch: INPUT_STRING
|COMPARISON_STRING_1 = OUTPUT_STRING_1
|COMPARISON_STRING_2 = OUTPUT_STRING_2
|COMPARISON_STRING_3 = OUTPUT_STRING_3
|COMPARISON_STRING_4 = OUTPUT_STRING_4
|DEFAULT_OUTPUT_STRING
}}
With static text

Here are a few basic examples of the #switch parser function in action.

  • {{#switch: a | a = A | b = B | #default = C}} outputs A. Since the input string equals a, the parser function outputs the corresponding output string, which is A.
  • {{#switch: b | a = A | b = B | #default = C}} outputs B. Since the input string equals b, the parser function outputs the corresponding output string, which is B.
  • {{#switch: z | a = A | b = B | #default = C}} outputs C. Since the input string equals z, and the parser function statement does not list z as one of its comparison strings, the parser function outputs its default output string, which is C.
With placeholder text

In templates, the #switch parser function is useful because it can adjust a template's behavior depending on the parameter's value.

For example, imagine that the wiki page called Template:FavoritePlace contains the following wikitext.

''Toa {{{toa_name}}}'s favorite place is {{#switch: {{{toa_name}}}
|Jaller
|Tahu=Ta-Wahi
|Gali=Ga-Wahi
|Lewa=Le-Wahi
|Pohatu=Po-Wahi
|Nuparu
|Onua=Onu-Wahi
|Kopaka=Ko-Wahi
|#default=not anywhere I know of
}}.''

Imagine transcluding the template with the snippet below.

{{FavoritePlace|toa_name=Pohatu}}

In that case, the template would produce the following text.

Toa Pohatu's favorite place is Po-Wahi.

The template's #switch parser function checked what the placeholder text {{{toa_name}}} was equal to. Since the parameter toa_name was set to Pohatu, the placeholder text {{{toa_name}}} was replaced with Pohatu. As a result, the parser function outputted its output string for Pohatu, which was Po-Wahi.

Next, imagine transcluding the template with the snippet below.

{{FavoritePlace|toa_name=Nuparu}}

In that case, the template would produce the following text.

Toa Nuparu's favorite place is Onu-Wahi.

The template's #switch parser function checked what the placeholder text {{{toa_name}}} was equal to. Since the parameter toa_name was set to Nuparu, the placeholder text {{{toa_name}}} was replaced with Nuparu. As a result, the parser function outputted its output string for Nuparu. That string was the same as the output string for Onua, which was Onu-Wahi.

Finally, imagine transcluding the template with the snippet below.

{{FavoritePlace|toa_name=Takanuva}}

In that case, the template would produce the following text.

Toa Takanuva's favorite place is not anywhere I know of.

The template's #switch parser function checked what the placeholder text {{{toa_name}}} was equal to. Since the parameter toa_name was set to Takanuva, the placeholder text {{{toa_name}}} was replaced with Takanuva. The parser function did not have any comparison strings equal to Takanuva, so it outputted its default output string, which was not anywhere I know of.

See also