Help:Magic words

From Tardis Wiki, the free Doctor Who reference
(Redirected from User:Najawin/Magic words)
Magic words 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 magic words 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 functions. 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[[edit source]]

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.

Code Notes Effect
{{CURRENTMONTH}} Gives current month 11
{{CURRENTMONTHNAME}} Gives current English name for month November
{{CURRENTDAY}} Gives current day 23
{{CURRENTDAYNAME}} Gives current day of week English name Saturday
{{CURRENTYEAR}} Gives current year 2024
{{CURRENTTIME}} Gives current time, in UTC 21:34
{{PAGENAME}} Returns the current page's name Magic words
{{FULLPAGENAME}} Returns the current page's full name Help:Magic words
{{NUMBEROFARTICLES}} Gives # of articles in main namespace containing a link and not a redirect 114,220

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 {{{Director}}}. Note that in wiki syntax, a user-defined variable is indicated by three curly braces {{{variable}}}, not two.

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

Behavior switches[[edit source]]

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:

  • __NOTOC__ (can appear anywhere in the wikitext; suppresses the table of contents)
  • __FORCETOC__ (can appear anywhere in the wikitext; makes a table of contents appear in its normal position above the first header)
  • __TOC__ (places a table of contents at the word's position)
  • __NOEDITSECTION__ (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__ (tells search engines to index the page)
  • __NOINDEX__ (tells search engines not to index the page)
DEFAULTSORT and DISPLAYTITLE[[edit source]]

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

  • {{DISPLAYTITLE:title}} (changes the displayed form of the page title)
  • {{DEFAULTSORT:sortkey}} (sets a default category sort key)

On this wiki, you should really use {{retitle}} or {{title dab away}} instead of straight up DISPLAYTITLE. On story pages, the latter italicizes the name of the story, leaving the dab term automatically unitalicized. One good use for {{retitle}} is IPod: in order to make it properly render as having a lower-case "i" at the beginning of the word iPod.

DEFAULTSORT is a particularly important magic word, and can be found working behind the scenes at {{NameSort}} and {{TitleSort}}. The former sorts a person by their last name, and the latter applies if an article begins with an article, such as "a", "an" or "the", and truncates it from that article's sortkey. Thus, Steven Moffat should have {{NameSort}} at the bottom, and The Girl in the Fireplace should have {{TitleSort}}.

Parser functions[[edit source]]

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[[edit source]]

A conditional 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:

  • {{#expr: expression }} (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."
  • {{#if: test string | value if non-empty | value if empty }} (selects one of two values based on whether the test string is empty)
  • {{#ifeq: string 1 | string 2 | value if equal | value if unequal }} (selects one of two values based on whether the test strings are equal – numerically if applicable)
  • {{#iferror: test string | value if error | value if correct }} (selects value based on whether the test string generates a parser error)
  • {{#ifexpr: expression | value if true | value if false }} (selects value based on evaluation of expression)
  • {{#ifexist: page title | value if exists | value if doesn't exist }} (selects value depending on whether a page title exists)
  • {{#switch: test | case1 = value for case 1 | ... | default }} (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:

{{#ifeq: | {{PAGENAME}} | Doctor Who (1996) | :''For other uses of the name ''Doctor Who'', please see [[Doctor Who (disambiguation) |}}
  1. #ifeq: says "look at the first two things and see if they're equal"
  2. "{{PAGENAME}}" 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: {{#ifeq:|{{PAGENAME}}|Doctor Who (1996)|:''For other uses of the name ''Doctor Who'', please see [[Doctor Who (disambiguation)|This is not Doctor Who (1996)}} But that would be rather silly.

Non-conditional[[edit source]]

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.

{{#titleparts: pagename | number of segments to return | first segment to return }}

If the number of segments parameter is not specified, it defaults to "0", which returns all the segments. If the first segment 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:

{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls}}Talk:An Unearthly Child/Episodes/Cave of Skulls
{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | 1 }}Talk:An Unearthly Child
{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | 2 }}Talk:An Unearthly Child/Episodes
{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | 2 | 2 }}Episodes/Cave of Skulls

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

{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | | -1 }} Cave of Skulls

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[[edit source]]

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.

Fandom's advice on magic words[[edit source]]