Manual wiki/Palabras mágicas/La función switch

Índice de la sección
«Palabras mágicas»

Para más información visite Switch parser function

Esta función compara un valor de entrada con varios casos de prueba, devolviendo una cadena asociada si se encuentra una coincidencia.

{{#switch:''comparison string''
| ''case'' = ''result''
| ''case'' = ''result''
| ''...''
| ''case'' = ''result''
| ''default result''

#switch con etiquetas de transclusión parcial puede afectar a un archivo de configuración que permite a un editor que no esté familiarizado con la codificación de plantillas ver y editar elementos configurables.

Valores predeterminados editar

The default result is returned if no case string matches the comparison string:

{{#switch: test | foo = Foo | baz = Baz | Bar }} Bar

In this syntax, the default result must be the last parameter and must not contain a raw equals sign (an equals sign without {{}}).

If it does, it will be treated as a case comparison, and no text will display if no cases match.

This is because the default value has not been defined (is empty).

If a case matches however, its associated string will be returned.

{{#switch: test | Bar | foo = Foo | baz = Baz }} →
{{#switch: test | foo = Foo | baz = Baz | B=ar }} →
{{#switch: test | test = Foo | baz = Baz | B=ar }} → Foo
{{#switch: ''comparison string''
| ''case'' = ''result''
| ''case'' = ''result''
| ''...''
| ''case'' = ''result''
| #default = ''default result''

Alternatively, the default result may be explicitly declared with a case string of "#default".

Default results declared in this way may be placed anywhere within the function:

{{#switch: test | foo = Foo | #default = Bar | baz = Baz }} Bar

If the default parameter is omitted and no match is made, no result is returned:

{{#switch: test | foo = Foo | baz = Baz }}

Grouping results editar

{{#switch: ''comparison string''
| ''case1'' = ''result1''
| ''case2'' 
| ''case3'' 
| ''case4'' = ''result234''
| ''case5'' = ''result5''
| ''case6'' 
| ''case7'' = ''result67''
| #default = ''default result''

It is possible to have 'fall through' values, where several case strings return the same result string. This minimizes duplication.

Here cases 2, 3 and 4 all return result234; cases 6 and 7 both return result67.

The "#default = " in the last parameter may be omitted in the above case.

Use with parameters editar

The function may be used with parameters as the test string.

In this case, it is not necessary to place the pipe after the parameter name, because it is very unlikely that you will choose to set a case to be the string "{{{parameter name}}}".

This is the value the parameter will default to if the pipe is absent and the parameter doesn't exist or have a value.

{{#switch: {{{1}}} | foo = Foo | baz = Baz | Bar }}

In the above case, if {{{1}}} equals foo, the function will return Foo.

If it equals baz, the function will return Baz.

If the parameter is empty or does not exist, the function will return Bar.

As in the section above, cases can be combined to give a single result.

{{#switch: {{{1}}} | foo | zoo | roo = Foo | baz = Baz | Bar }}

Here, if {{{1}}} equals foo, zoo or roo, the function will return Foo.

If it equals baz, the function will return Baz.

If the parameter is empty or does not exist, the function will return Bar.

Additionally, the default result can be omitted if you do not wish to return anything if the test parameter value does not match any of the cases.

{{#switch: {{{1}}} | foo = Foo | bar = Bar }}

In this case, the function returns an empty string unless {{{1}}} exists and equals foo or bar, in which case it returns Foo or Bar, respectively.

This has the same effect as declaring the default result as empty.

{{#switch: {{{1}}} | foo | zoo | roo = Foo | baz = Baz | }}

If for some reason you decide to set a case as "{{{parameter name}}}", the function will return that case's result when the parameter doesn't exist or doesn't have a value. The parameter would have to exist and have a value other than the string "{{{parameter name}}}" to return the function's default result.

(when {{{1}}} doesn't exist or is empty):
{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }} Foo
(when {{{1}}} has the value "test"):
{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }} Bar
(when {{{1}}} has the value "{{{1}}}"):
{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }} Foo

In this hypothetical case, you would need to add the pipe to the parameter ({{{1|}}}).

Comparison behavior editar

As with #ifeq, the comparison is made numerically if both the comparison string and the case string being tested are numeric; or as a case-sensitive string otherwise:

{{#switch: 0 + 1 | 1 = one | 2 = two | three}} → three
{{#switch: {{#expr: 0 + 1}} | 1 = one | 2 = two | three}} → one
{{#switch: 02 | +1 = one | +2 = two | three}} → two
{{#switch: 100 | 1e1 = ten | 1e2 = hundred | other}} → hundred
{{#switch: a | a = A | b = B | C}} → A
{{#switch: A | a = A | b = B | C}} → C

A case string may be empty:

{{#switch: | = Nothing | foo = Foo | Something }}Nothing

Once a match is found, subsequent cases are ignored:

{{#switch: b | f = Foo | b = Bar | b = Baz | }}Bar

Numerical comparisons with #switch and #ifeq are not equivalent to comparisons in expressions (see also above):

{{#switch: 12345678901234567 | 12345678901234568 = A | B}} → B
{{#ifexpr: 12345678901234567 = 12345678901234568 | A | B}} → A

Raw equal signs editar

"Case" strings cannot contain raw equals signs. To work around this, use the {{=}} magic word, or replace equals sign with HTML code =.

You type You get
{{#switch: 1=2
 | 1=2 = raw
 | 1=2 = nowiki
 | 1{{=}}2 = template
 | default
{{#switch: 1=2
 | 1=2 = html
 | default
For a simple real life example of the use of this function, check Template:NBA color. Two complex examples can be found at Plantilla:Ll and w:Template:BOTREQ.

Replacing #ifeq editar


#switch can be used to reduce expansion depth.

For example
  • {{#switch:{{{1}}} |condition1=branch1 |condition2=branch2 |condition3=branch3 |branch4}}

is equivalent to

  • {{#ifeq:{{{1}}}|condition1 |branch1 |{{#ifeq:{{{1}}}|condition2 |branch2 |{{#ifeq:{{{1}}}|condition3 |branch3 |branch4}}}}}}

i.e. deep nesting, linear:


On the other hand, the switch replacement could be complicated/impractical for IFs nested in both branches (shown with alternatives of indentation, indented on both sides), making full symmetrical tree:

Índice de la sección «Palabras mágicas»

  Introducción  Opciones de comportamiento  Variables de fecha y hora  Variables de metadatos técnicos  Variables de estadísticas  Variables de espacios de nombres  Otras variables  Funciones de metadatos  Funciones de datos de URL  Funciones de espacios de nombres  Funciones de formato  Funciones de localización  Funciones modificadoras de transclusión  Otras funciones  La función expr  La función if  La función ifeq  La función iferror  La función ifexpr  La función ifexist  La función rel2abs  La función switch  La función time  La función timel  La función titleparts

Manual wiki
Los proyectos WikimediaEdiciónUsando el entorno wikiAnexo