Predefinição:Options
A common options mechanism for templates.
Usage
Initialization
The configuration of options must be registered before the options can be used. It can be done via {{options/init}}, for example(It will be used for all following examples as well):
{{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}} -->}}
It is possible to have it load automatically("autoload") via Template:<namespace>/initOptions
. For example, for {{options|icon|small}}
, it will try to transclude Template:item/initOptions
.
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/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>}}