Template:Infobox Var/doc: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
 
Line 1: Line 1:
{{documentation}}
{{Documentation}}
{{invokes|Infobox Item}}
{{ToC|left}}
{{clear}}


{{Standard infobox parameters}}
The '''Var''' template is used to store data in the cache for later use.


==Usage==
==Parameters==
<pre>{{Var|name|data}}</pre>


===Name===
===type===
The name of the variable. Case sensitive.
The type of Var, either "Varbit" or "Varplayer".


===Data===
===index===
The index of the Var, which should always be a number.
The data to be stored in the variable. The literal value is stored and is not evaluated (unless you also store the appropriate functions) - if you store 6*7, when you recall it you will get the result <code>6*7</code>, not 42; if you store <code><nowiki>{{#expr:6*7}}</nowiki></code> you ''will'' get the result <code>42</code>.


==Recalling data==
===name===
The name of the Varbit or Varplayer. There are no official names, but names should generally reflect the usage of the Var. This param is optional, and if it is not included, the name defaults to "Varbit X" or "Varplayer X". If used on pages other than <nowiki>RS:Varbit/X</nowiki> or <nowiki>RS:Varplayer/X</nowiki>, note that this param should be set in order to avoid strange default names being generated from the title of the page.
Recalling data is done by use of the #var parser function:
<pre>{{#var:name}}</pre>
where ''name'' is the same named used in the var template above.


===content===
==Expansion on usage==
The content that the Var is used for, as a link. If the Var is used in multiple pieces of content, this should be explained in prose with this param set to "Multiple". If the Var is used in a way that would not be considered content, this param should be set to "Other".
Variables can be redefined where needed - you can use the Var template many times.


===class===
Variables can be called where needed - you can use the #var parser function many times.
The class of the Var, which describes how the Var's value is interpreted. The following classes are supported with categories.

* '''Enum''' - Denotes a Var where each value is interpreted as a different state.
Variables can be defined recursively, that is defined as a function as themselves. For example:
* '''Switch''' - Denotes a Var with two values, typically 0 and 1, where one state is interpreted as ON and the other OFF.
<pre>{{Var|i|0}}
* '''Bitmap''' - Denotes a Var where the value of the Var is not interpreted as a single unit. Instead, each bit or groups of bits in the Var are interpreted separately.

* '''Counter''' - Denotes a Var where the value is interpreted as a counter, usually counting up or down incrementally.
{{Var|i|{{#expr:{{#var:i}}+1}}}}</pre>
* '''Other''' - Denotes a Var which does not fall into any of the above categories.
The first line initialises the variable i to 0 (required to prevent expression errors), and the second will increment i by one each time it is used. If you place the second line into a template that is used multiple times on a page e.g. a table row template, you can recall i to find something that may be of importance in context e.g. the number of rows in the template. As an additional note, the increment doesn't have to be 1, it can be any number - it doesn't even have to be a fixed number, it can be a function of some other parameters in the template.

==Rationale==
''"But I know how to use the #vardefine parser function. Isn't this the exact same thing?"''

Yes, it is. The Var templates uses the #vardefine parser function to define the variables. So what's the point of this template?

A problem with the design of the variables extension is that vardefine is always executed even if it is on the false path of a conditional parser function: for example, if you created a construct such as
<pre>{{#ifexpr: ...
|{{#vardefine:a|b}}
|{{#vardefine:a|c}}
}}</pre>
Variable a will now always hold c, regardless of whether the expression is true or false.

There are two ways around this. The first is to move the #vardefine to the outside of the conditional parser: following the previous example, the required construct would be similar to

<pre>{{#vardefine:a|{{#ifexpr: ... |b|c}}}}</pre>

However, sometimes you can't move the #vardefine outside, it logically makes more sense or is easier to follow to leave the #vardefine on the inside. This is where the Var template comes in. As the template is not expanded if it is on the false path, the #vardefine is not present and cannot define variables where it shouldn't. Thus a construct like this will also work for the above example:
<pre>{{#ifexpr: ...
|{{Var|a|b}}
|{{Var|a|c}}
}}</pre>


==See also==
[[mw:Extension:VariablesExtension|The Variables Extension page on MediaWiki]]

<includeonly>[[Category:Transclusion templates|{{PAGENAME}}]]</includeonly>

Latest revision as of 11:44, 17 October 2024

This is a documentation subpage for Template:Infobox Var.
It contains usage information, categories, and other content that is not part of the original template page.
Template:Infobox Var invokes function main in Module:Infobox Var using Lua.
Template:Infobox Var invokes Module:Infobox Item using Lua.


Standard infobox parameters

This infobox uses Module:Infobox, which provides a number of standard parameters.

Versioning

This infobox supports switching between multiple versions. Each version needs to have a name defined in a parameter called version#, numbered sequentially starting from 1. For all parameters that have different values, a number can be appended to the end of each parameter name to define the value for each version. Parameters without a version number will be shared between all versions.

For example, if the name of version 2 is called "Bob", you should define |name2 = Bob.

Linking

You can link to a page and have an alternative infobox appear by using a hash value, as if you were linking to a section. The hash value is the version name, as defined by the relevant version# parameter. For example, to link to the variant of a switch infobox with |version3 = Quest, you can use [[Example#Quest]].

Alternative versions are not visible or accessible when Javascript is disabled.

defver

If defver is defined, the infobox will switch to the specified version when the page is loaded. For example, |defver = 4 will automatically select the 4th infobox version.

smwname

This overrides the name of the Semantic MediaWiki subobject that the infobox will set the parameters into. By default, an infobox with multiple versions will save properties into subobjects with the same name as the version name. This parameter only needs to be set if the version name is different from the desired subobject name, or if an unversioned infobox needs to set properties of a specific subobject (for example, when versioned and unversioned infoboxes of different types, like Item and Bonuses, are used on the same page).

This is necessary when a version name would otherwise be an invalid SMW subobject name. The value of smwname is also not permitted to be an invalid subobject name. A subobject name must not violate the following rules:

  • The first 5 characters of the name must not contain a period (.).
  • The name must not be the number zero (0).
  • The name must not be the empty string.


If the infobox does not have multiple versions, SMW properties would normally be set on the page itself. Setting smwname overrides this default behaviour and the properties will be only be set to the specified subobjects and not the page itself.

Multiple subobjects can be specified by separating the subobject names with the broken bar character (¦). The properties will be set in all of the subobjects specified.

For example, if the infobox has |version2 = H.A.M. member, this would cause an invalid subobject name due to having a period within the first 5 characters. The infobox should therefore override the smwname using |smwname2 = HAM member or something similar.

Suppressing SMW

Setting |smw = No will suppress all SMW output for the infobox.

Setting |smwX = No will suppress all SMW output for version number X.

__dump__

This parameter is used only for debugging an infobox. If __dump__ is present with any value then a string version of the infobox object is dumped to the page, rather than displaying the infobox as normal. This should only be used when previewing the page and must never be saved in a mainspace page. Setting this adds Category:Dumping infoboxes to the page.

Specifically, this shows the result at the time the infobox is converted to a string. This will always occur when the module finally returns, but may instead occur earlier in the code. The object is dumped using mw.dumpObject(box). Dumping will not prevent a script error occurring.


Parameters

type

The type of Var, either "Varbit" or "Varplayer".

index

The index of the Var, which should always be a number.

name

The name of the Varbit or Varplayer. There are no official names, but names should generally reflect the usage of the Var. This param is optional, and if it is not included, the name defaults to "Varbit X" or "Varplayer X". If used on pages other than RS:Varbit/X or RS:Varplayer/X, note that this param should be set in order to avoid strange default names being generated from the title of the page.

content

The content that the Var is used for, as a link. If the Var is used in multiple pieces of content, this should be explained in prose with this param set to "Multiple". If the Var is used in a way that would not be considered content, this param should be set to "Other".

class

The class of the Var, which describes how the Var's value is interpreted. The following classes are supported with categories.

  • Enum - Denotes a Var where each value is interpreted as a different state.
  • Switch - Denotes a Var with two values, typically 0 and 1, where one state is interpreted as ON and the other OFF.
  • Bitmap - Denotes a Var where the value of the Var is not interpreted as a single unit. Instead, each bit or groups of bits in the Var are interpreted separately.
  • Counter - Denotes a Var where the value is interpreted as a counter, usually counting up or down incrementally.
  • Other - Denotes a Var which does not fall into any of the above categories.