Dungeons and Dragons Wiki:Programming Templates

If you don't know how a template works, this is the wrong page to be at. This page is a quick tutorial on ParserFunctions, using a healthy number of examples for easy copypasta.

Variables
Variables are encapsulated around triple curly parenthesis (like  ). 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.

If you want to give a default value for if the variable does not exist, treat the variable as a template with the first parameter as the value to use instead. The default default value is the name of the variable. If you want no value, use an empty value for the first parameter. This is especially useful in statements.

Examples

 * Use a variable name:
 * Give the variable name a default value of Bob:
 * Show nothing if variable name isn't used:
 * Detect whether a variable name is used:

Tips
The only difference between the following is stylistic. Use whichever is easier to understand.



Switches
If you have a list of options allowed.

If possible, an array map should be used instead.

ArrayMap
Example:

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.

By default, the output separating token is the same as the input separating token. This can be changed, as per the next example.

Example:

Result:

Equality Testing
If you want to check to see if some variable or 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  . Note that the variable defaults to nothing if it doesn't exist.

Numeric/String Equality Testing
 

#pos
Returns position (starting with 0) of where the search term begins. Returns an empty string "" if not found.

Useful Escaping
&amp;#91; and &amp;#93; 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.