Help:Magic words: Difference between revisions

From Tardis Wiki, the free Doctor Who reference
m
upholding T:CLEAN CODE
m (Robot: Adding Category:Help)
m (upholding T:CLEAN CODE)
Line 1: Line 1:
'''← [[Tardis:Help]]'''
'''[[Tardis:Help]]'''


'''{{PAGENAME}}''' 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.  
'''{{PAGENAME}}''' 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.  
Line 8: Line 8:


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.   
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====
==== 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''.   
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''.   


Line 41: Line 41:
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.
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====
==== Behavior switches ====
Another type of magic word is the behavior switch.  These are indicated by the format <nowiki>__BEHAVIORSWITCHNAME__</nowiki>.  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:
Another type of magic word is the behavior switch.  These are indicated by the format <nowiki>__BEHAVIORSWITCHNAME__</nowiki>.  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:
*<tt><nowiki>__NOTOC__</nowiki></tt> (can appear anywhere in the wikitext; suppresses the table of contents)
* <tt><nowiki>__NOTOC__</nowiki></tt> (can appear anywhere in the wikitext; suppresses the table of contents)
*<tt><nowiki>__FORCETOC__</nowiki></tt> (can appear anywhere in the wikitext; makes a table of contents appear in its normal position above the first header)
* <tt><nowiki>__FORCETOC__</nowiki></tt> (can appear anywhere in the wikitext; makes a table of contents appear in its normal position above the first header)
*<tt><nowiki>__TOC__</nowiki></tt> (places a table of contents at the word's position)
* <tt><nowiki>__TOC__</nowiki></tt> (places a table of contents at the word's position)


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


*<tt><nowiki>__NOGALLERY__</nowiki></tt> (on a category page, replaces thumbnails with normal links)
* <tt><nowiki>__NOGALLERY__</nowiki></tt> (on a category page, replaces thumbnails with normal links)
*<tt><nowiki>__HIDDENCAT__</nowiki></tt> (on a category page, makes it a [[wikipedia:WP:HIDDENCAT|hidden category]])
* <tt><nowiki>__HIDDENCAT__</nowiki></tt> (on a category page, makes it a [[wikipedia:WP:HIDDENCAT|hidden category]])
*<tt><nowiki>__INDEX__</nowiki></tt> (tells search engines to index the page)
* <tt><nowiki>__INDEX__</nowiki></tt> (tells search engines to index the page)
*<tt><nowiki>__NOINDEX__</nowiki></tt> (tells search engines not to index the page)
* <tt><nowiki>__NOINDEX__</nowiki></tt> (tells search engines not to index the page)
=====DEFAULTSORT and DISPLAYTITLE=====
===== DEFAULTSORT and DISPLAYTITLE =====
Additionally, there are two very important behavior switches that impact variables.  They might be thought of as "variable behavior switches".   
Additionally, there are two very important behavior switches that impact variables.  They might be thought of as "variable behavior switches".   


*<tt><nowiki>{{DISPLAYTITLE:</nowiki>''title''<nowiki>}}</nowiki></tt> (changes the [[wikipedia:WP:DISPLAYTITLE|displayed form]] of the page title)
* <tt><nowiki>{{DISPLAYTITLE:</nowiki>''title''<nowiki>}}</nowiki></tt> (changes the [[wikipedia:WP:DISPLAYTITLE|displayed form]] of the page title)
*<tt><nowiki>{{DEFAULTSORT:</nowiki>''sortkey''<nowiki>}}</nowiki></tt> (sets a default category [[wikipedia:WP:SORTKEY|sort key]])
* <tt><nowiki>{{DEFAULTSORT:</nowiki>''sortkey''<nowiki>}}</nowiki></tt> (sets a default category [[wikipedia:WP:SORTKEY|sort key]])




Line 72: Line 72:
</pre>
</pre>
This will automatically sort the article alphabetically in whatever categories it is placed into.
This will automatically sort the article alphabetically in whatever categories it is placed into.
===Parser functions===
=== 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.   
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.   
However, there are two major type of parser function, conditional and non-conditional.   


====Conditional function====
==== 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:
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><nowiki>{{#expr:</nowiki> ''expression'' <nowiki>}}</nowiki></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><nowiki>{{#expr:</nowiki> ''expression'' <nowiki>}}</nowiki></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><nowiki>{{#if:</nowiki> ''test string'' | ''value if non-empty'' | ''value if empty'' <nowiki>}}</nowiki></tt> (selects one of two values based on whether the test string is empty)
* <tt><nowiki>{{#if:</nowiki> ''test string'' | ''value if non-empty'' | ''value if empty'' <nowiki>}}</nowiki></tt> (selects one of two values based on whether the test string is empty)
*<tt><nowiki>{{#ifeq:</nowiki> ''string 1'' <nowiki>|</nowiki> ''string 2'' <nowiki>|</nowiki> ''value if equal'' <nowiki>|</nowiki> ''value if unequal'' <nowiki>}}</nowiki></tt> (selects one of two values based on whether the test strings are equal – numerically if applicable)
* <tt><nowiki>{{#ifeq:</nowiki> ''string 1'' <nowiki>|</nowiki> ''string 2'' <nowiki>|</nowiki> ''value if equal'' <nowiki>|</nowiki> ''value if unequal'' <nowiki>}}</nowiki></tt> (selects one of two values based on whether the test strings are equal – numerically if applicable)
*<tt><nowiki>{{#iferror:</nowiki> ''test string'' <nowiki>|</nowiki> ''value if error'' <nowiki>|</nowiki> ''value if correct'' <nowiki>}}</nowiki></tt> (selects value based on whether the test string generates a parser error)
* <tt><nowiki>{{#iferror:</nowiki> ''test string'' <nowiki>|</nowiki> ''value if error'' <nowiki>|</nowiki> ''value if correct'' <nowiki>}}</nowiki></tt> (selects value based on whether the test string generates a parser error)
*<tt><nowiki>{{#ifexpr:</nowiki> ''expression'' <nowiki>|</nowiki> ''value if true'' <nowiki>|</nowiki> ''value if false'' <nowiki>}}</nowiki></tt> (selects value based on evaluation of expression)
* <tt><nowiki>{{#ifexpr:</nowiki> ''expression'' <nowiki>|</nowiki> ''value if true'' <nowiki>|</nowiki> ''value if false'' <nowiki>}}</nowiki></tt> (selects value based on evaluation of expression)
*<tt><nowiki>{{#ifexist:</nowiki> ''page title'' | ''value if exists'' | ''value if doesn't exist'' <nowiki>}}</nowiki></tt> (selects value depending on whether a page title exists)
* <tt><nowiki>{{#ifexist:</nowiki> ''page title'' | ''value if exists'' | ''value if doesn't exist'' <nowiki>}}</nowiki></tt> (selects value depending on whether a page title exists)
*<tt><nowiki>{{#switch:</nowiki> ''test'' | ''case1'' = ''value for case 1'' | ... | ''default'' <nowiki>}}</nowiki></tt> (provides alternatives based on the value of the test string)
* <tt><nowiki>{{#switch:</nowiki> ''test'' | ''case1'' = ''value for case 1'' | ... | ''default'' <nowiki>}}</nowiki></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:
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:
:<nowiki>{{#ifeq: | {{PAGENAME}} | Doctor Who (1996) | :''For other uses of the name ''Doctor Who'', please see [[Doctor Who (disambiguation) |}}</nowiki>
:<nowiki>{{#ifeq: | {{PAGENAME}} | Doctor Who (1996) | :''For other uses of the name ''Doctor Who'', please see [[Doctor Who (disambiguation) |}}</nowiki>


#'''<nowiki>#ifeq:</nowiki>''' says "look at the first two things and see if they're equal"
# '''<nowiki>#ifeq:</nowiki>''' says "look at the first two things and see if they're equal"
#'''<nowiki>"{{PAGENAME}}"</nowiki>''' returns the value of the current page
# '''<nowiki>"{{PAGENAME}}"</nowiki>''' returns the value of the current page
#'''"Doctor Who (1996)"''' is the value we're trying to match
# '''"Doctor Who (1996)"''' is the value we're trying to match
#'''<nowiki>":''For other uses . . . "</nowiki>''' is the event we want to happen if the first two things are equal
# '''<nowiki>":''For other uses . . . "</nowiki>''' is the event we want to happen if the first two things are equal
#'''"|}}"''' 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: <nowiki>{{#ifeq:|{{PAGENAME}}|Doctor Who (1996)|:''For other uses of the name ''Doctor Who'', please see [[Doctor Who (disambiguation)|This is not Doctor Who (1996)}}</nowiki> But that would be rather silly.
# '''"|}}"''' 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: <nowiki>{{#ifeq:|{{PAGENAME}}|Doctor Who (1996)|:''For other uses of the name ''Doctor Who'', please see [[Doctor Who (disambiguation)|This is not Doctor Who (1996)}}</nowiki> But that would be rather silly.
====Non-conditional====
==== 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'''.
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'''.


Line 107: Line 107:
Let's imagine that we had made our [[Talk:An Unearthly Child|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:
Let's imagine that we had made our [[Talk:An Unearthly Child|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><nowiki>{{#titleparts:</nowiki> Talk:An Unearthly Child/Episodes/Cave of Skulls<nowiki>}}</nowiki></tt> &rarr; '''{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls }}'''
:<tt><nowiki>{{#titleparts:</nowiki> Talk:An Unearthly Child/Episodes/Cave of Skulls<nowiki>}}</nowiki></tt> '''{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls }}'''
:<tt><nowiki>{{#titleparts:</nowiki> Talk:An Unearthly Child/Episodes/Cave of Skulls <nowiki>| 1 }}</nowiki></tt> &rarr; '''{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | 1 }}'''
:<tt><nowiki>{{#titleparts:</nowiki> Talk:An Unearthly Child/Episodes/Cave of Skulls <nowiki>| 1 }}</nowiki></tt> '''{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | 1 }}'''
:<tt><nowiki>{{#titleparts:</nowiki> Talk:An Unearthly Child/Episodes/Cave of Skulls <nowiki>| 2 }}</nowiki></tt> &rarr; '''{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | 2 }}'''
:<tt><nowiki>{{#titleparts:</nowiki> Talk:An Unearthly Child/Episodes/Cave of Skulls <nowiki>| 2 }}</nowiki></tt> '''{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | 2 }}'''
:<tt><nowiki>{{#titleparts:</nowiki> Talk:An Unearthly Child/Episodes/Cave of Skulls <nowiki>| 2 | 2 }}</nowiki></tt> &rarr; '''{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls| 2 | 2 }}'''
:<tt><nowiki>{{#titleparts:</nowiki> Talk:An Unearthly Child/Episodes/Cave of Skulls <nowiki>| 2 | 2 }}</nowiki></tt> '''{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls| 2 | 2 }}'''


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


:<tt><nowiki>{{#titleparts:</nowiki> Talk:An Unearthly Child/Episodes/Cave of Skulls <nowiki>| | -1 }}</nowiki></tt> &rarr; ''' [[{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | | -1 }}]]'''  
:<tt><nowiki>{{#titleparts:</nowiki> Talk:An Unearthly Child/Episodes/Cave of Skulls <nowiki>| | -1 }}</nowiki></tt> ''' [[{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | | -1 }}]]'''  


Now, you might be thinking, "How does that save any time whatsoever? I could just type <nowiki>[[Cave of Skulls]]</nowiki> 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.
Now, you might be thinking, "How does that save any time whatsoever? I could just type <nowiki>[[Cave of Skulls]]</nowiki> 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.
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===
=== 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.
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:Help:Extension:ParserFunctions]]
*[[mw:Extension:StringFunctions]]
* [[mw:Extension:StringFunctions]]
*[[metawikipedia:Help:Variable]]
* [[metawikipedia:Help:Variable]]
*[[mw:Manual:Magic words]]
* [[mw:Manual:Magic words]]


===Wikia's advice on magic words===
=== Wikia's advice on magic words ===


[[Category:The Hub]]
[[Category:The Hub]]
[[Category:Help]]
[[Category:Help]]
Bots, Bureaucrats, emailconfirmed, Administrators
765,429

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.