MediaWiki templates
From WhyNotWiki
This is mostly about how to write an use templates -- the technical how-to. For information about how you should use templates on this site, see WhyNotWiki:Conventions, and for a list of available templates, see [...].
Contents |
[edit] Writing templates
[edit] Complaints about / Limitations of / Problems with MediaWiki's template technology
[edit] Only has one data type: strings!
So if you want to pass in as arguments and intelligently process other types of variables/data, like arrays, you're pretty much out of luck.
For some data types -- like numbers -- it's not a big problem, because strings can be converted to numbers as needed (for example by #expr:).
But what about arrays?
What if you wanted to pass in an array of software projects to the {{software metadata}} template, and for eachsoftware project that was passed in, I wanted another template to be called, or wanted to add the calling page to a category, ...
Example: I want to do something like this:
{{software metadata
|name = Ruby
|based_on = Perl, Lisp, Ada
}}
and have the template generate a based_on relation for each item passed into the based_on argument. I'd maybe envision the template code to do that to look something like this:
based_on.map { |a|
Relation.new(a)
}
Unfortunately, there's no concept of lists or sets or arrays in MediaWiki, and thus also no concept of iteration over a set.
We could allow for a fixed number of items, and then use a bunch of if statements to use just the ones that are available...
{{software metadata
|name = Ruby
|based_on_1 = Perl
|based_on_2 = Lisp
|based_on_3 = Ada
}}
But that's highly inelegant and doesn't scale well.
[edit] No variables
So you can't store the result of some calculation in a variable and use it again in two places; instead, you'd have to duplicate the entire expression that produced that value, in both places!
[edit] No loops
Or maybe there's a limited {{loop}} template, but ... it's probably very limited if so!
[edit] Ugly-looking templates
Often very compressed together, because any whitespace would be regarded as a newline character, which often needs to be avoided.
[edit] Calling/including/transcluding/using templates
[edit] How to call a template from within an argument to another template
[Workarounds (category)] for: [MediaWiki / Bugs (category)][MediaWiki / Problems (category)]
You have to use | to separate arguments inside of your inner template call; otherwise, the | that you use there will be interpreted as argument separators for the outer template call.
This works:
{{quote| Text of quote | {{Bible{{!}}2Kings{{!}}5:11-14}} }}
| “ | Text of quote | ” |
But this doesn't:
{{quote| Text of quote | {{Bible|2Kings|5:11-14}} }}
| “ | Text of quote | ” |
[edit] Using a built-in "variable" like {{PAGENAME}} in a template call
This is helpful if you want to make your documentation as generic as possible so that you can copy and paste it into other templates without having to make many modifications afterwards.
So if you're on {{Foo}}, you can simply call {{Template:{{PAGENAME}} }} in your documentation and it will actually transclude Foo.
This didn't work for me:
{{{{PAGENAME}}}}
but this did:
{{Template:{{PAGENAME}} }}
[edit] Other
[edit]
http://en.wikipedia.org/wiki/Wikipedia:Template_namespace -- information on what templates are and how to use them
[edit] Sources of good templates / Nice template indexes
http://en.wikipedia.org/wiki/Category:Wikipedia_templates
http://en.wiktionary.org/wiki/Wiktionary:Index_to_templates
http://en.wikipedia.org/wiki/Wikipedia:Template_messages
http://www.wikia.com/wiki/List_of_shared_templates
http://wiki.videolan.org/List_of_templates
http://www.theopedia.com/List_of_templates
http://lostpedia.com/wiki/Lostpedia:List_of_Templates
http://www.fmepedia.com/index.php/Fmepedia_templates
[edit] Sharing templates across multiple wikis
Help:Shared templates - Wikia Central (http://www.wikia.com/wiki/Help:Shared_templates).
Templates on the Central Wikia can be used on any other Wikia by using {{wikicities:templatename}}.
One advantage of having shared templates on the Central Wikia is that they are watched by a large community, and can be quickly reverted in cases of vandalism.
How do they do that?? That might be handy to do sometime...
http://commons.wikimedia.org/wiki/Category:Commons_templates (not sure if these are "shared" or not)
[edit] Templates available on this wiki
Templates edit (Category edit)
How to get a list of templates: http://whynotwiki.com/index.php?title=Special:Prefixindex&&from=&namespace=10
See also: Conventions for using templates
[edit] Raw SQL
http://en.wikipedia.org/wiki/Wikipedia:List_of_templates_by_usage
This table shows the 200 templates which are most linked to by other templates.
SELECT CONCAT('[[Template:',templatelinks.tl_title,']]') AS title, COUNT(templatelinks.tl_title) AS count FROM templatelinks, page WHERE templatelinks.tl_namespace = 10 AND templatelinks.tl_from = page.page_id AND page.page_namespace = 10 GROUP BY title ORDER BY count DESC LIMIT 200
[edit] Examples of interesting templates
(Template sightings)
http://en.wiktionary.org/w/index.php?title=Template:timeline&action=edit (used on http://en.wiktionary.org/wiki/mitigate)
http://en.wikipedia.org/w/index.php?title=Template:Image_label&action=edit (used by http://en.wikipedia.org/wiki/Template:German_Federal_States_Labelled_Map)
