Modèle:Options
This template provides a system to configure the default parameter values of other templates. This is useful in two cases:
- A template is transcluded many times. {{options}} reduces the amount of duplicated code.
- For instance, consider a list of 100 {{item}} transclusions that should all have the
|showid=yesparameter. Instead of adding this parameter to all 100 {{item}}s, {{options}} makes it possible to define|showid=yesas the new default for {{item}}.
- For instance, consider a list of 100 {{item}} transclusions that should all have the
- A template is transcluded as part of another template which does not expose its parameters. {{options}} allows to influence them nonetheless.
- For instance, {{diving equipment}} uses {{item}} internally, but does not expose all of {{item}}'s parameters, meaning it is not possible to use
{{diving equipment|showid=yes}}, for example. By defining|showid=yesas the new default for {{item}}, however, the internal transclusions in {{diving equipment}} are automatically affected.
- For instance, {{diving equipment}} uses {{item}} internally, but does not expose all of {{item}}'s parameters, meaning it is not possible to use
Usage
Initialization
{{options}} requires a definition of which parameters are modifiable via it and in what way. This is done by transcluding {{options/init}} on a /initOptions subtemplate. For example, the options behavior of Template:Item is defined on Template:Item/initOptions. See Category:Options subtemplates for a list of all of these subtemplates.
{{options/init}} accepts any number of parameters, in the following format:
| key: <option> = <default value>
| alias: <option> = <key alias>
| value: <option>:<value> = <value alias>
}}
- First unnamed parameter
This is the "namespace",[a] identifying the template for which the options are being defined. In almost all cases, this should simply be the name of the template.
- key:
Parameters starting with key: are the most important ones. They define an option along with its default value. For instance, | key: size = large defines the option size with a default value of large. The default value can also be omitted. In addition, there are three predefined default values with special meanings: (bool), (bool:true), and (bool:false) – see below for information.
It is possible to define any number of key: parameters, as many as options are needed.
- alias:
For convenience and better intuitiveness, aliases for options can be created using parameters starting with alias:. For instance, | alias: proportion = size defines proportion as an alias for size. As a result, either one of the two can be used to refer to the same option.
It is possible to define any number of aliases, including multiple for the same option.
- value:
Option values can have aliases as well, via parameters starting with value:. For instance, | value: size:big = large defines big as an alias for large – though only for the size option! (Note that there must not be a space between the option name and the value alias.) {{options}} would now interpret big and large to mean the same thing when used as values for the size option.
Again, any number of value aliases can be defined, including multiple for the same value. Beware not to use option aliases here, however, as that will not work. For instance, with the example from above, | value: proportion:big = large would not work, as proportion is merely an option alias.
Example
{{options/init|item
<!-- Options initialization for the "item" namespace -->
<!-- Option "maxsize" -->
|key:maxsize= <!-- Default value: (none) -->
|alias:maxdimensions=maxsize <!-- Option alias: "maxdimensions" -->
<!-- Option "scale" -->
|key:scale=1 <!-- Default value: 1 -->
<!-- Option "showid" -->
|key:showid=no <!-- Default value: "no" -->
|alias:showitemid=showid <!-- Option alias: "showitemid" -->
|value:showid:n=no <!-- Value alias for "showid=no": "showid=n" -->
|value:showid:false=no <!-- Value alias for "showid=no": "showid=false" -->
}}
Boolean value aliases
Option values are commonly of the Boolean type (true/false). Often multiple value aliases are accepted for these options, like y and yes. Therefore, there is a shortcut within {{options/init}} for this common behavior:
| key: <option> = (bool)
This syntax automatically sets up <option> with the value aliases yes and on for y, as well as no and off for n. Defining all these aliases manually can be avoided this way.
For example, |key:showid=(bool) means that showid=y, showid=yes, and showid=on are all identical. Analogously, showid=no and showid=off are identical to showid=n.
Note that the option has no default value when using the code above. To define one, use (bool:true) or (bool:false):
| key: <option> = (bool:true)- For example,
|key:showid=(bool:true)means thatshowid=yis the default and thatshowid=yesandshowid=onare aliases for it. Furthermore,showid=n,showid=no, andshowid=offare all interpreted asshowid=(blank).
- For example,
| key: <option> = (bool:false)- For example,
|key:showid=(bool:false)means thatshowid=nis the default and thatshowid=noandshowid=offare aliases for it. Furthermore,showid=y,showid=yes, andshowid=onare all interpreted asshowid=(blank).
- For example,
Get/set options
Get:
{{options/get|<namespace>|<key>}} or {{options|<namespace>|<key>}}
Set:
{{options/set|<namespace>|<key1>=<value1>|<ke2>=<value2>|...}} or {{options|<namespace>|<key1>=<value1>|<key2>=<value2>|...}}
Note: The latter form is a bit slow, but more handy for writing. For using in templates the former is prefer, and for using in articles both are ok.
Example:
{{options/init|item<!-- init config for "item" namespace
-->|key:icons=yes<!-- declare an option named "icons", and its default value is "yes"
-->|key:small=no<!-- default value of "small" option is "no"
-->|value:small:n=no<!-- value "n" for "small" is same as "no", so {{options/set|item|small=n}} is same as {{options/set|item|small=no}}
-->|key:style=<!-- default value of "style" is empty value.
-->|alias:css=style<!-- "css" is an alias of "style", so {{options|item|css}} is same as {{options|item|style}}
-->}}
{{options/get|item|icons}}<!-- get default value -->
{{options/set|item|icons=no}}<!-- set it to "no" -->
{{options/get|item|icons}}<!-- now it will be "no" -->
{{options/set|item|icons=all|small=yes}}<!-- set multiple options -->
{{options/get|item|icon}}
{{options/get|item|small}}
It will output:
yes no all yes
Snapshot
It is possible to save current state of options to a snapshot, then restore it later.
{{options/snap|<namespace>|<snpashot_name>}}
{{options/restore|<namespace>|<snpashot_name>}}
Example:
{{options/set|item|icons=aa|small=bb}}<!--
-->{{options|item|icons}} {{options/get|item|small}} <!-- aa bb
-->{{options/snap|item|save1}}<!--
-->{{options/set|item|icons=xx|small=yy}}<!--
-->{{options|item|icons}} {{options/get|item|small}} <!-- xx yy
-->{{options/restore|item|save1}}<!--
-->{{options|item|icons}} {{options/get|item|small}} <!-- aa bb -->
It will output:
aa bb xx yy aa bb
There is a special snapshot created during initialization, it can be restored via {{options/reset|<namespace>}}
Notes
- In theory, the transclusion of {{options/init}} does not have to be on a
/initOptionssubtemplate. It is merely a convention, to allow {{options}} to obtain the initialization automatically:{{options|<namespace>|...}}, when transcluded for the first time on a page, will attempt to transcludeTemplate:<namespace>/initOptions. {{options/init}} could be transcluded anywhere, however.- That said, there does not seem to be a use case for such non-standard usage. It is best to stick to the standard
/initOptionsway.
- That said, there does not seem to be a use case for such non-standard usage. It is best to stick to the standard
Subpages
Footnotes
- ↑ Unrelated to the MediaWiki concept of namespaces.