Dungeons and Dragons Wiki:Programming Templates

From Dungeons and Dragons Wiki
Revision as of 04:37, 10 February 2011 by Havvy (talk | contribs) (Expansion)
Jump to: navigation, search

If you don't know how a template works, this is the wrong page to be at. Combining the important information of SMW, ParserFunctions, and general wikimedia templating is what needs to happen here.

Variables

Variables are encapsulated around triple curly parens (like {{{var}}}). If the page is called via transclusion, variables will be replaced with the required text, as long as they are specifically passed. If the variable is not passed to it, the text will be the default, which is by default, the name of the variable surrounded by three curly braces. If you want a separate default, add a pipe key after the variable name followed by what should be the default. Other variables can be used in the default.

Switches

If you have a list of options allowed.

{{#switch: {{{varname}}}
|value = What to show if |varname = value
|value2 = What to show if |varname = value2
|default value (if you want to show the value of the var, have this just be {{{varname}}}
}}

If possible, an array map should be used instead.

ArrayMap

{{#arraymap: {{{varName}}}|separating token|stringName|Display for each string}}
Example:
{{#arraymap: {{{desc}}}|, |x|[[Descriptor::x]]}}

In the example, an array map is created for the variable "desc" with each string in the array separated by ", " in the template usage page. The variable to reference each string is called "x" in this array. For each string, it gives it the Descriptor SMW property.

The separating token cannot be modified or hidden. It is probably best to use ", " as the separating token.

Equality Testing

If you want to check to see if some variable or
Some subquery has no valid condition.
expression exists or returns a result, use the #if function. #if: asks for three expressions. The first expression should evaluate to either character data or whitespace. The second expression is what to show if the first expression evaluates to character data. The third expression is what to show if the first expression evalutates to whitespace only.

The most common case of using #if: is to check to see if a variable exists. The syntax for doing so is {{#if: {{{varname|}}} | Show if variable exists | Show if variable doesn't exist. }}. Note that the variable defaults to nothing if it doesn't exist.

Numeric/String Equality Testing

{{#ifeq: string 1 | string 2 | value if identical | value if different }}

String Functions

#len

{{#pos:string|search term|offset}}

Useful Escaping

[ and ] display as [ and ] , respectively, but won't get parsed as wiki links.

{{|}} and {{=}} return the pipe and equals sign respectively.

Substitution Testing

If you add the string subst: before the name of a transcluded page, it will replace the transclusion call with the text of the transclusion at the time of page-creation. This is useful for when the contents of a template don't cause premature end of another template call. It is also useful for seeing what is truly parsed when looking for why an error is occurring.