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.
Contents
Variables[edit]
Variables are encapsulated around triple curly parenthesis (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.
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 {{#if: }} statements.
Examples[edit]
- Use a variable name
- {{{name}}}
- Give the variable name a default value of Bob
- {{{name|Bob}}}
- Show nothing if variable name isn't used
- {{{name|}}}
- Detect whether a variable name is used
- {{#if: {{{name|}}} | Name Exists. | Name doesn't exist.}}
Tips[edit]
The only difference between the following is stylistic. Use whichever is easier to understand.
- {{{name|}}}
- {{#if: {{{name|}}} | {{{name}}}}}
Switches[edit]
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[edit]
{{#arraymap: {{{varName|}}}|input separating token|stringName|Display for each string|output separating token}}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.
By default, the output separating token is the same as the input separating token. This can be changed, as per the next example.
Example:{{#arraymap: Item 1, Item 2|,|x|x|:}}
Result: Item 1:Item 2
Equality Testing[edit]
If you want to check to see if some variable or {{#ask:}} 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[edit]
{{#ifeq: string 1 | string 2 | value if identical | value if different }}
String Functions[edit]
#pos[edit]
{{#pos:string|search term|offset}}
Returns position (starting with 0) of where the search term begins. Returns an empty string "" if not found.
Useful Escaping[edit]
[ and ] display as [ and ] , respectively, but won't get parsed as wiki links.
{{|}} and {{=}} return the pipe and equals sign respectively.
Substitution Testing[edit]
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.
Back to Main Page → Getting Started