Working With MediaWiki
like "
{{Painting|Composition with Red, Yellow and Blue|Piet Mondrian}}
" would display the following:
Composition with Red, Yellow and Blue is a painting by Piet Mondrian.
Numbered and named parameters can be combined in the same template, which can be useful in certain situations, though it’s not usually recommended. If a template is passed both named and unnamed values, then the parameters {{{1}}}, {{{2}}} etc. will get the 1st, 2nd and so on values that do not have a name assigned to them — not necessarily the 1st, 2nd etc. values.
There’s one more tag that can be used in templates — . This tag is a little trickier than and , because it affects text outside of the tag itself. If you add one or more tags to a template page (or to a regular page that’s transcluded), then only the text within the tags gets displayed in other pages. All other text — whether or not it’s part of or tags — is ignored.
Uses for templates
There are generally six main usages for templates within MediaWiki: (1) tags to highlight problems within a page, (2) infoboxes, (3) navigation boxes, (4) structural elements, (5) formatting aids and (6) templates used as formatting aids directly by various extensions.
The "Needs work" template was an example of a problem-highlighting tag . Infoboxes are well-known to readers of Wikipedia; they usually appear on the right-hand side of the page, and show important information about the page’s subject. Figure 4.1 shows one example from the English Wikipedia.
Figure 4.1 An infobox from the English-language Wikipedia
Navigation boxes typically have no parameters; on Wikipedia, they’re usually found on either the bottom or the side of the page, and show a large set of links to related pages. You can see an example of it from the English Wikipedia in Figure 4.2.
Figure 4.2 A navigation box from the English-language Wikipedia
Templates can also be used to display standard structural elements of a page; a common example on the English-language Wikipedia is the “Reflist” template, which is basically just a wrapper around the Cite extension’s tag function, providing some additional formatting options.
Templates can be used to do inline formatting of text and images, to simplify work for editors. An example is the "Convert" template on the English Wikipedia, which lets you convert a value in a certain unit to another unit — like converting pounds to kilograms. Another interesting example is the “Chess diagram” template, which can display an entire chessboard layout. There are hundreds of other such templates on Wikipedia.
Finally, there are extensions in which templates are used to format values — a template name is passed to a function, which then substitutes in to that template other values that it has generated. Semantic MediaWiki is one extension that uses such a trick, with its "template" results format. We’ll talk more about that last usage in later parts of the book. And we’ll also talk quite a bit about infobox-style templates, which are a key building block for the Semantic MediaWiki system.
Parser and tag functions
Pre-defined calls within MediaWiki are all referred to as “magic words”. There are three kinds of magic words: parser and tag functions, variables, and behavior switches. We’ll get to parser and tag functions first.
Parser and tag functions are both pre-defined functions that take in a set of inputs and display a result, or otherwise perform some action. The main difference between the two is that parser functions are called by wrapping the function with curly braces, while tag functions are called using angled brackets, in the manner of HTML or XML tags.
As an example, let’s take a (fictitious) function, ’#reverse’, that takes in a string and displays its characters in reverse. If it were a parser function, a call to it could look like:
{{#reverse:abc}}
...or:
{{#reverse:string=abc}}
In other words, a parser function can be defined as either taking in named or unnamed parameters. If they’re unnamed parameters, they’re defined by the order in which they’re called.
If ’reverse’ were a tag function, on the other hand, a call to it could look like this:
abc
...or this:
In all of these cases, the call would produce the string “cba” on the page.
Just as with HTML and
Weitere Kostenlose Bücher