Help:Templates (advanced)

From Guild Wars 2 Wiki
Jump to navigationJump to search

This article will introduce the tools which can make templates extremely flexible; it is recommended to familiarize yourself with the basics before delving into these.

Something to remember when writing templates is that a future person, who may or may not be you, will likely end up having to tweak the template in the future. This might be due to newly released content, or fixing plain old bugs and corner cases. With this in mind try to lay out the template sufficiently clearly for somebody else to be able to understand it.

Hidden comments[edit]

Comments can be hidden in raw wikicode just like in regular HTML with arrow syntax:

Text displayed on the page. <!-- this is a hidden comment -->

Text displayed on the page.

Magic words[edit]

Magic words are provided by core mediawiki.

Need to know functions:

  • {{PAGENAME}} — Displays the name of the current page. Especially useful in templates used on multiple pages.
  • {{NAMESPACE}} — Displays the name of the current namespace. Most pages are in the main article namespace (i.e. there isn't any prefix on the page title, unlike this page, which is prefixed "Help:" because it is in the help namespace). In most cases, you won't want to set things like Categories if a template is used outside of the main article namespace.
  • {{lc: {{{input|}}} }}, {{uc: {{{input|}}} }} and {{ucfirst: {{{input|}}} }} — Changes the case of input text to be lowercase, uppercase, and uppercase the first letter, respectively.

Parser functions[edit]

Parser functions are provided by the ParserFunctions extension. These functions are often used with parameterised inputs to change the output of a template.

Need to know functions:

  • {{#if: {{{input|}}} | output if non-blank | output if blank }} — Checks if the input is blank or not
  • {{#ifeq: {{{input1|}}} | {{{input2|}}} | output if equal | output if not equal }} — Checks if the input is equal to a given value
  • {{#switch: {{{input|}}} | option1 = output 1 text | option2 | option 3 = output 2 or 3 text | #default = default output text }} — Checks if the input is equal to multiple different options. Multiple inputs can result in the same output. Specifying a default output is optional.
  • {{#expr: 8 / 3 round 1 }} — Performs a numerical calculation. There are loads of keywords including: floor, ceil, mod, round, /, *, +, -.

The rest of the functions on the mediawiki help page are useful too but are seen far less often than the above four.

Variables[edit]

Variables are provided by mw:Extension:Variables. They are good for storing a cleaned input parameter, and then avoiding repetition of functions later in the same template.

The functions:

  • {{#vardefine: variable name | value }} — Stores a value against a given name, but does not display the stored value on the page.
  • {{#vardefineecho: variable name | value }} — Stores a value against a given name, but does display the stored value on the page.
  • {{#var: variable name }} — Displays the value of a previously stored variable. Optionally a default fallback value can be specified with {{#var: variable name | fallback value }}

Arraymaps[edit]

The #arraymap: function is provided by the Page Forms extension. They are the easiest way to implement for loops in wikicode.

In the example below, the arraymap template is used to split a (magenta) comma separated list of ingredients. The symbol used to perform the split appears in the next parameter (red). Each ingredient is interpreted as "@@@" (orange), a randomly chosen sequence of characters unlikely to appear in prose (this could be any series of characters, e.g. $$$, >:D, or $keyword). Any wikitext can then be used in the output part (green). Finally the output parts are delimited with the newline character (cyan).

{{#arraymap: Apple, Banana, Chocolate Bar, Dill Sprig |,|@@@|
* [[@@@]]
|\n}}

Example[edit]

Putting all of the above together:

<!-- Define the members of Destiny's Edge but don't show them -->
{{#vardefine: Destiny's Edge | Rytlock, Logan, NORN LADY, That Asura midget }}

The memorable members of Destiny's Edge are:

<!-- Loop through each comma separated member -->
{{#arraymap: {{#var: Destiny's Edge }} |,|@@@|

<!-- Create a numbered list using the # sign, and do a lookup to correct each given NPC -->
# {{#switch: {{lc: @@@ }}
| logan = Logan Thackeray
| rytlock = Rytlock Brimstone
| that asura midget = Zojja and her golem Mr. Sparkles
| norn lady
| eir = Eir and Garm
}}

<!-- Separate the results with a newline -->
|\n}}


The memorable members of Destiny's Edge are:

  1. Rytlock Brimstone
  2. Logan Thackeray
  3. Eir and Garm
  4. Zojja and her golem Mr. Sparkles

See also[edit]