Help:Magic words

← Tardis:Help

 are powerful bits of wiki coding which seem to operate by magic. They are called "magic" because they do things with one simple word that conceals rather a lot of complicated coding. Most editors of wikis, even those very experienced with template coding, don't really understand the underlying code that makes them work. Hence, they are "magic" because, as was once remarked in Battlefield, any sufficiently advanced technology is indistinguishable from magic.

It is important to note that unlike templates we create on this wiki for our use, they are not categorized anywhere, least of all at Category:Templates. In a sense, they are so fundamental to the coding of a wiki that they are hidden, much like the code which makes brackets link text, as happens when you type Doctor Who.

There are three main typs of magic word: variables, behavior switches, and parser fucntions. String functions are sometimes considered a separate category, but they are really just a specialized parser function, and so we lump the two together on this wiki.

Although this article cannot possibly hope to explain every aspect of magic words in detail, they are absolutely essential to the coding of truly powerful templates. At the end of this article, a list of good, detailed articles is given for you to explore the subject in greater detail.

Variables
Some of the simplest magic words have to do with detecting dates and other numbers. Because they return values which are not constant, this type of magic word is called a variable.

These are not variables in the sense of being "user definable". The user has no control over the content of the variable. The term variable has another wiki code meaning, particularly in templates. If one wishes to elicit a response from a user in a template, such as by requesting the input of a director's name on an infobox, one would use a variable such as. Note that in wiki syntax, a user-defined variable is indicated by three curly braces, not two.

It should also be pointed out that these are not the only variable magic words that exist. Consult the standard Wikia chart at the bottom of this page for more.

Behavior switches
Another type of magic word is the behavior switch. These are indicated by the format __BEHAVIORSWITCHNAME__. Note the syntax requires a one-word name in all caps enclosed in two underscores. No other variation will work. Here are some of the most common ones:
 *   (can appear anywhere in the wikitext; suppresses the table of contents)
 *   (can appear anywhere in the wikitext; makes a table of contents appear in its normal position above the first header)
 *   (places a table of contents at the word's position)


 *   (hides the section edit links beside headings)
 *  __NEWSECTIONLINK__  (adds a "+" link for adding a new section on a non-"Talk" page)
 *  __NONEWSECTIONLINK__  (removes the "+" link on "Talk" pages)


 *  __NOGALLERY__  (on a category page, replaces thumbnails with normal links)
 *  __HIDDENCAT__  (on a category page, makes it a hidden category)
 *  __INDEX__ </tt> (tells search engines to index the page)
 *  __NOINDEX__ </tt> (tells search engines not to index the page)

DEFAULTSORT and DISPLAYTITLE
Additionally, there are two very important behavior switches that impact variables. They might be thought of as "variable behavior switches".


 *  </tt> (changes the displayed form of the page title)
 *  </tt> (sets a default category sort key)

DISPLAYTITLE could be used more often at this wiki than it is. For instance, the title of articles at the top of the page could be made to be italicized in the case of book titles, for instance. However, there is no consensus on this, as yet. One article that does use DISPLAYTITLE is IPod, in order to make it properly render as having a lower-case "i" a the beginning of the word iPod. DISPLAYTITLE is a rather finicky bit of code, however, and will only accept certain changes to the title. For instance, it happily accepts iPod, but balks ad iPOd.

DEFAULTSORT is a particularly important magic word to understand, as it should be placed on every page you create, if the title of that page is a person's name, or an article that begins with an article, such as "a", "an" or "the".

Thus, the articles Steven Moffat and The Girl in the Fireplace, should have this notation at the bottom:

This will automatically sort the article alphabetically in whatever categories it is placed into.

Parser functions
Parser functions are magic words that get the wiki software to perform an action upon a string (that is a set of letters, numbers and/or symbols) in order to return a result. Explaining all their possible functions is well beyond this scope of this help page.

However, there are two major type of parser function, conditional and non-conditional.

Conditional function
A condtional function is one that results in an action based upon some sort of if/then/else logic. Here's a list of conditional functions with a very basic explanation of each:


 *  </tt> (evaluates the given expression; see wikipedia:Help:Calculation)  While not strictly conditional, it's often nested within a conditional statement so as to answer the question, "If the result of this calculation is something, then take this action; otherwise, do this other thing."
 *  </tt> (selects one of two values based on whether the test string is empty)
 *  </tt> (selects one of two values based on whether the test strings are equal – numerically if applicable)
 *  </tt> (selects value based on whether the test string generates a parser error)
 *  </tt> (selects value based on evaluation of expression)
 *  </tt> (selects value depending on whether a page title exists)
 *  </tt> (provides alternatives based on the value of the test string)

These are not very useful within the space of a normal article. However, they are used extensively in templates. The utility of #ifeq should be readily apparent. For example, here's a silly little example which puts together variables and conditional expressions into a single statement. If we wanted to put a code on every episode page until it found Doctor Who (1996) and put up a disambiguation note this is what we'd do:


 * 1)  #ifeq:  says "look at the first two things and see if they're equal"
 * 2)  ""  returns the value of the current page
 * 3) "Doctor Who (1996)" is the value we're trying to match
 * 4)  ":''For other uses . . . "  is the event we want to happen if the first two things are equal
 * 5) "|}}" says "if the first two things are unequal do nothing".  If we wanted the code to take a positive action on inequality, we'd type something between the pipe and the curly brackets. So, we could write:  But that would be rather silly.

Non-conditional
Non-conditional parser functions have exactly the same syntax as conditional functions, but they don't actually depend on if/then logic to produce the result. No attempt will be made here to even list all the possible parser and string functions. But we will examine one example: #titleparts.

This function separates a pagetitle into segments based on slashes, then returns some of those segments as output.


 *  </tt>

If the number of segments</tt> parameter is not specified, it defaults to "0", which returns all the segments. If the first segment</tt> parameter is not specified or is "0", it defaults to "1".

Let's imagine that we had made our talk page for An Unearthly Child a little more complicated than it actually is. Consider what would happen if we gave each individual episode a subpage of its own. This, then, is how we could use #titleparts to manipulate the subpage titles:


 *  </tt> → 
 *  </tt> → 
 * <tt> </tt> → 
 * <tt> </tt> → 

Perhaps most usefully, we could use a negative number to actually find the name of the episode, and link to it.


 * <tt> </tt> →  

Now, you might be thinking, "How does that save any time whatsoever? I could just type Cave of Skulls and be done with it." And yes, you could. But within a template this becomes extremely powerful. With some additional coding, you could fairly easily create something that would work on any page automatically — without you having to type it on every single story page on the wiki.

And, indeed, most of the value of parser functions is not in their use on an individual page. Rather, they are the building blocks for coding templates that automatically find, manipulate, and most importantly link to information on any page.

Detailed info on magic words
If you want to explore the world of template coding with magic words such as parser functions, there are a few pages you'll want to look at.


 * mw:Help:Extension:ParserFunctions
 * mw:Extension:StringFunctions
 * metawikipedia:Help:Variable
 * mw:Manual:Magic words