Template:T

From Wikipedia of the Dark Brotherhood, an online Star Wars Club

{{{1|pqr}}}

Examples

Code Result
{{T}}
pqr
{{T|}}
{{T|abc}}
abc
{{T|abc|def}}
abc
{{T|1=abc|1=def}}
def
{{T|abc|1=def}}
def
{{T|1=abc|def}}
def
{{T|{{T}}}}
pqr
{{T|{{T|{{T}}}}}}
pqr
{{T|{{T|{{T|{{T}}}}}}}}
pqr
{{T|a{{T|b}}}}
ab
{{T|{{T|a=b}}}}
pqr
{{T|a=b}}
pqr
{{T|1=a=b}}
a=b

How it works (simple example)

This template is a clever utility that simply outputs a value. However, by leveraging some quirks in MediaWiki's parser, we use it as a "hacky" conditional switch to output text in dynamic ways.

A fairly straightforward example is Template:User timezone (used occasionally on user pages, such as Orv's).

If you look at the source code for {{User timezone}}, you will see a chain of calls like {{T | 1=UTC-12 | 1{{{UTC-12|}}}=}} for all the different UTC numbers. Each call gets analyzed and outputs a value.

Here is exactly how that chain evaluates based on user input:

Scenario 1: The variable is activated

Let's say on your user page you add {{User timezone|UTC-5=true}}. Here is what happens inside {{T | 1=UTC-5 | 1{{{UTC-5|}}}=}}:

  • The first parameter is given the name 1 and holds the value UTC-5.
  • Because you defined UTC-5=true, the template variable {{{UTC-5|}}} evaluates to true.
  • Therefore, the second parameter evaluates to 1true= (which holds a blank string: 1true=).
  • Before executing, MediaWiki "squishes" (normalizes) the parameters. Because 1 and 1true are completely unique parameter names, nothing is overwritten.
  • {{T}} outputs the value of parameter 1. Since it remains untouched, the template outputs UTC-5.

Scenario 2: The variable is ignored

Now let's look at how the other calls in the chain are handled. Assuming you still only activated UTC-5, here is what happens to {{T | 1=UTC-6 | 1{{{UTC-6|}}}=}}:

  • The first parameter is given the name 1 and holds the value UTC-6.
  • Because you did NOT define UTC-6=true, the template variable {{{UTC-6|}}} does not exist and evaluates to nothing.
  • Therefore, the second parameter evaluates to 1= (which holds a blank string: 1=).
  • We now have two parameters named the exact same thing: 1=UTC-6 and 1=.
  • This is where the parameter "squishing" kicks in. MediaWiki only allows unique parameter names and always takes the last one declared. The blank 1= overrides the 1=UTC-6.
  • {{T}} outputs the value of parameter 1. Since it was overwritten with a blank string, the output for this specific call is entirely blank.