Template talk:Switch

From Wikipedia, the free encyclopedia

Contents

Documentation - Discussion

{{Switch}} is protected at the moment. Use {{Editprotected}} on this page to propose an edit.  

[edit] Documentation

{{switch|value|case: 1=result 1|case: 2=result 2|etc.}} shows one specific branch out of multiple possible branches, dependent on a given value. It requires MediaWiki version 1.6 or better.

[edit] Usage

{{switch
 |VALUE-TO-BE-TESTED
 | case: foo=hello
 | case: bar=world
 | default=Neither ''foo'' nor ''bar''
}}

where VALUE-TO-BE-TESTED is a parameter or a variable

If no default result is defined, the result if no value matches will be blank.

The variables to switch have some constraints because the value must be able to be used as a parameter name
Characters known not to work are: =|
There mustn't be any space between the pipe character and the first character.
There is no fall-through (if you look at the code, you know why)
The similar but less ingenious parser function #switch: is more flexible.

[edit] Example

Code Result
{{switch
  |{{CURRENTDOW}}
  |case: 1=Mon
  |case: 2=Tue
  |case: 3=Wed
  |case: 4=Thu
  |case: 5=Fri
  |case: 6=Sat
  |case: 0=Sun
  |default=No date
}}
Fri

[edit] How "switch" works

It works by the principle that a parameter declaration can be defined by the value of another parameter.

When we access the parameter, we don't have the name hard coded, but we rely on the value of another parameter (in this case the parameter 1).

In its simplest form it is just like {{booleq/eq2}} ({{{{{{2|}}}|}}}), but to make it more user friendly and remove the risk trying to access parameter 1, the string case: was added to the parameters to check for.

Also there is an additional parameter default that can be used if you want to have a default value (no other case value matches).

The resulting code is:

{{{case: {{{1|}}}|{{{default|}}}}}}

[edit] See also

[edit] Discussion

Contents

Add issues below as you see fit, sign with ~~~~

[edit] Sandbox

Yesterday was Thursday. Today is Friday. Tomorrow will be Saturday.


Today is Friday.

Today is the 14th day of the month.

Today is whitespace-sensitivity day.

[edit] interwikis

the page is protected. pleas add pt:Predefinição:Switch

Done. --CBDunkerson 11:05, 5 April 2006 (UTC)

[edit] Working in 1.6 only

It's only working from 1.6 and up, due to that parameter twiddling, and use of defaults for parameters. Would be nice to mention it in the <noinclude> part. I've added the template to use at the top of this page, in the meantime (hoping nobody minds). jae 12:12, 10 April 2006 (UTC)

Okay, maybe look at the Rfd on Meta for m:Category:Version 1.6, that's where m:Help:Array and m:Help:Advanced templates are listed at the moment. I've added links to these help pages here. -- Omniplex 11:49, 1 May 2006 (UTC)