Manual wiki/Palabras mágicas/La función switch
- 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'' }}
- Ejemplos
{{#switch:baz|foo=Foo|baz=Baz|Bar}}
→ Baz{{#switch:foo|foo=Foo|baz=Baz|Bar}}
→ Foo{{#switch:zzz|foo=Foo|baz=Baz|Bar}}
→ Bar
#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
editarThe 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
editarThe 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
editarAs 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 =
.
- Example
You type | You get |
---|---|
{{#switch: 1=2 | 1=2 = raw | 1=2 = nowiki | 1{{=}}2 = template | default }} |
template |
{{#switch: 1=2 | 1=2 = html | default }} |
html |
Replacing #ifeq
editar{{#ifeq:{{{1}}}|condition1 |<!--then-->branch1 |<!--else-->{{#ifeq:{{{1}}}|condition2 |<!--then-->branch2 |<!--else-->{{#ifeq:{{{1}}}|condition3 |<!--then-->branch3 |<!--else-->branch4}}}}}}
#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:
{{#ifeq:{{{1}}}|condition1 |<!--then-->branch1t{{ #ifeq:{{{1}}}|condition2 |<!--then-->branch1t2t{{#ifeq:{{{1}}}|condition4|<!--then-->branch1t2t4t|<!--else-->branch1t2t4e}} |<!--else-->branch1t2e{{#ifeq:{{{1}}}|condition5|<!--then-->branch1t2e5t|<!--else-->branch1t2e5e}} }} |<!--else-->branch1e{{#ifeq:{{{1}}}|condition3 |<!--then-->branch1e3t{{#ifeq:{{{1}}}|condition6|branch1e3t6t|branch1e3t6e}} |<!--else-->branch1e3e{{ #ifeq:{{{1}}}|condition7 |branch1e3e7t |branch1e3e7t }} }} }}
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:
Manual wiki Índice • Introducción • Enlaces | |||||||||||||||
Los proyectos Wikimedia • Edición • Usando el entorno wiki • Anexo | |||||||||||||||