Declaring variables?

Fragment of a discussion from Project:Support desk
Jump to: navigation, search
#vardefine is way easier: {{#ifeq: {{{param|}}} | foo | {{#vardefine:width|50}} {{#vardefine:color|green}} | (else) }}. Then use {{#var:width}} and {{#var:color}}. --Subfader 16:20, 21 February 2011 (UTC)
Theaitetos21:12, 25 February 2011
If you do not have access to the VariablesExtension, you can make use of the switch functions ability to have "fall through" values:
{{#switch: {{{param}}}
 | case1 = result1
 | case2 
 | case3 
 | case4 = result2
 | case5 = result3
 | case6 
 | case7 = result4
 | default result
}}
Here cases 2, 3 and 4 all return result2; cases 6 and 7 both return result4, e.g. set results like this:
{{#switch: {{{param}}}
 | case1 = <div style="color: red; width: 50px">{{{Text}}}</div>
 | case2 
 | case3 
 | case4 = <div style="color: red; width: 75px">{{{Text}}}</div>
 | case5 = <div style="color: red; width: 150px">{{{Text}}}</div>
 | case6 
 | case7 = <div style="color: red; width: 200px">{{{Text}}}</div>
 | <div style="color: red; width: 100px">{{{Text}}}</div>         /* this is the default value
}}
If you expect certain values for your parameters you have to write them all down anyway. This way you have to write down all combined parameters, which might be a lot. The other option is to use a second template inside the first one, and have your values as parameters in the implemented template call, e.g. if the second template is called {{Rainbow-2}}, then write {{Rainbow}} as follows:
{{#switch: {{{param}}}
 | case1 = {{Rainbow-2|Text={{{Text}}}|param={{{param}}}|color=purple}}
 | case2 
 | case3 
 | case4 = {{Rainbow-2|Text={{{Text}}}|param={{{param}}}|color=red}}
 | case5 = {{Rainbow-2|Text={{{Text}}}|param={{{param}}}|color=yellow}}
 | case6 
 | case7 = {{Rainbow-2|Text={{{Text}}}|param={{{param}}}|color=green}}
 | {{Rainbow-2|Text={{{Text}}}|param={{{param}}}|color=blue}}         /* this is the default value</div>
}}
Case 1 gives a purple text, case 2, 3 and 4 all red texts, and so forth. That way you can split the colors and width depending on the value of param. If, for example, you expect certain param patterns like "big red" or "little blue", then start the template with an {{#ifexpr: {{#pos:{{param}}|red}}+1 > 1 | {{Rainbow-2|param={{{param}}}|color=red}} }}, and similar for the other colors. Then have the second template Rainbow-2 make that test on the expected size-values of the {{param}} parameter, like {{#ifexpr: {{#pos:{{param}}|little}}+1 > 1 | <div style="color: red; width: 75px">{{{Text}}}</div> }}. --Theaitetos 21:12, 25 February 2011 (UTC)
Theaitetos21:12, 25 February 2011
 
Personal tools

Variants
Actions
Navigation
Support
Download
Development
Communication
Toolbox