Extension:ExpandTemplates
ExpandTemplates Release status: stable |
|
---|---|
Implementation | Special page |
Description | recursive expansion of templates, parser functions and variables |
Author(s) | Tim Starling |
MediaWiki | 1.14alpha r42593 + |
License | Any OSI approved license |
Download | |
Check usage and version matrix | |
Bugs: list open list all report |
The page Special:ExpandTemplates has two input boxes, one to enter wikitext, and another one to enter a pagename. It produces in the "Result" window the expanded wikitext, i.e., templates, parser functions and variables are expanded recursively; variables depending on pagename are expanded based on the supplied pagename. This is an intermediate result before a page is rendered after saving or pressing "Preview" (or the resulting wikitext is submitted to other processing), helpful in understanding and debugging wikitext expansion. It does not support substitution (a separate expansion process carried out first after entering wikitext in an edit window and pressing "Show review" or "Save"; the resulting wikitext can be inspected in an arbitrary edit window by pressing "Show changes", or by saving a test page and reopening the edit window).
In addition there is a preview area. However, this shows the rendered result of expanding the expanded wikitext again (bugzilla:28616). Although the result is usually the same as after single expansion, it can be different if the first expansion produces braces.
Template:! provides a way to allow "|" in a parameter value of a template or parser function, in particular for use in table syntax. This template can be used with ExpandTemplates to perform partial expansion/substitution.
The table, using Template:2x, shows that a template call with a parameter separator "|" in the form {{!}} prevents ordinary expansion; the last column shows the preview area (with the result of double expansion).
original wikitext | expanded wikitext | rendering | preview produced by ExpandTemplates |
---|---|---|---|
{{2x|[[MediaWiki]] }} |
[[MediaWiki]] [[MediaWiki]] |
MediaWiki MediaWiki | MediaWiki MediaWiki |
{{2x{{!}}[[MediaWiki]] }} |
{{2x|[[MediaWiki]] }} |
{{2x|MediaWiki }} | MediaWiki MediaWiki |
{{2x{{!{{!}}}}[[MediaWiki]] }} |
{{2x{{!|}}[[MediaWiki]] }} |
{{2x{{!|}}MediaWiki }} | {{2x|MediaWiki }} |
Contents |
Remove comments [edit]
The "Remove comments" option selects whether comments are removed, not just in the final result but throughout the expansion process. This affects the result of parser functions: a comment in the expression of #expr or #ifexpr gives an error message unless "Remove comments" is on, and comments affect #if and ifeq.
Example:
{{#expr:<!--p-->3}} {{#ifeq:<!--p-->3|3|1|0}} {{#if:<!--p-->|1|0}}
Normal expansion, and ExpandTemplates with "Remove comments" on:
3
1
0
Full substitution, and ExpandTemplates with "Remove comments" off:
Expression error: Unexpected < operator
0
1
XML parse tree [edit]
Optionally the XML parse tree (the XML DOM representation of the wikitext, also called document tree or DOM tree) is shown (see also Manual:Preprocessor_DOM.php). For example:
a [[b|c]] {{CURRENTYEAR}} {{{{{a}}}}} {{#ifeq:2|3|a|b}} {{#switch:a|a=3|b=4|a=5}} {{a|b|c=d[[e|f]]g|h}} {{{a|b}}} {{#tag:nowiki|a=b}} {{#tag:nowiki|aeb}}
gives
<root>a [[b|c]] <template lineStart="1"><title>CURRENTYEAR</title></template> <template lineStart="1"><title><tplarg><title>a</title></tplarg></title></template> <template lineStart="1"><title>#ifeq:2</title><part><name index="1"/><value>3</value></part><part><name index="2"/><value>a</value></part><part><name index="3"/><value>b</value></part></template> <template lineStart="1"><title>#switch:a</title><part><name>a</name>=<value>3</value></part><part><name>b</name>=<value>4</value></part><part><name>a</name>=<value>5</value></part></template> <template lineStart="1"><title>a</title><part><name index="1"/><value>b</value></part><part><name>c</name>=<value>d[[e|f]]g</value></part><part><name index="2"/><value>h</value></part></template> <tplarg lineStart="1"><title>a</title><part><name index="1"/><value>b</value></part></tplarg></root> <template lineStart="1"><title>#tag:nowiki</title><part><name>a</name>=<value>b</value></part></template> <template lineStart="1"><title>#tag:nowiki</title><part><name index="1"/><value>aeb</value></part></template>
Thus:
- The result shows a stage in processing before actual evaluation. Thus even names of variables, parser functions, and templates have not been determined. Also, the colon and "#" are not detected yet, so no distinction is made between variables, parser functions, and templates, let alone that there would be distinction between existing and non-existing variables, parser functions, and templates. Therefore:
- Distinction is made between parameter items with and without "=", even though for some items of some parser functions it is treated as plain text after all.
- "|a=b|a=c" is not reduced yet to "|a=c" because the fact that the left sides are equal is only known after their evaluation, and also because this reduction depends on the evaluation of title. For example, for titles of the form
#switch:
.., "|a=b|a=c" is equivalent to "|a=b" after all.
- A pair of double rectangular brackets shields "|" from being interpreted as a parameter separator, but links are not shown separately in the tree.
Input text in URL [edit]
The wikitext serving as input can also be put in the URL, e.g., using urlencode, {{fullurl:special:ExpandTemplates|input={{urlencode:{{texpr{{!}}abc}}}}}}
gives //www.mediawiki.org/w/index.php?title=Special:ExpandTemplates&input=%7B%7Btexpr%7Cabc%7D%7D. {{!}}
, using Template:!, prevents the template texpr from expanding in the URL, and also prevents "|" from being interpreted as a separator of the parameter of urlencode from a dummy second parameter. It can also be used in a template call without parameters, albeit that in the input box of ExpandTemplates this becomes a template with unnamed parameter {{{1}}} being the empty string instead of undefined; e.g. {{fullurl:special:ExpandTemplates|input={{tc{{!}}}}}}
. This does not work for variables and parser functions, use percent codes for one pair of braces instead (see wikitext); for the ParserFunctions prefix "#" use %23:
http://www.mediawiki.org/w/index.php?title=Special:ExpandTemplates&input={{CURRENTTIME}}.
http://www.mediawiki.org/w/index.php?title=Special:ExpandTemplates&input={{%23expr:2*3}}.
Query parameters:
- removecomments=true
- removenowiki=true
- generate_xml=true
Example:
Download [edit]
You can download the extension directly from the MediaWiki source code repository (browse code). You can get:
- One of the extensions tags
Not all extensions have tags. Some extensions have tags for each release, in which case those tags have the same stability as the release. To download a tag
- Go to the tags list
- Click the name of the tag you want to download
- Click "snapshot"
- The latest version of one of the extensions branches
Each extension has a master branch containing the latest code (might be unstable). Extensions can have further branches as well.
- Go to the branches list
- Click the branch name
- Click "snapshot"
- A snapshot made during the release of a MediaWiki version.
This might be unstable and is not guaranteed to work with the associated MediaWiki version.
After you've got the code, save it into the extensions/ExpandTemplates directory of your wiki.
If you are familiar with git and have shell access to your server, you can obtain the extension, with all its tags and branches, as follows:
cd extensions
git clone https://gerrit.wikimedia.org/r/p/mediawiki/extensions/ExpandTemplates.git
Installation [edit]
Add the following at the end of your LocalSettings.php:
require_once("$IP/extensions/ExpandTemplates/ExpandTemplates.php");
Adjusting the box width [edit]
By default the width of the input and result box is only 11 characters. You may want to change this to full width by adding something like the following to your CSS:
textarea { border: 1px solid #2f6fab; color: Black; background-color: white; width: 100%; padding: 0.1em; overflow: auto; }
See also [edit]
![]() |
This extension is being used on one or more Wikimedia projects. This probably means that the extension is stable and works well enough to be used by such high-traffic websites. Look for this extension's name in Wikimedia's CommonSettings.php and InitialiseSettings.php configuration files to see where it's installed. A full list of the extensions installed on a particular wiki can be seen on the wiki's Special:Version page. |