Extension:ExpandTemplates

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
ExpandTemplates

Release status: stable

Implementation Special page
Description recursive expansion of templates, parser functions and variables
Author(s) Tim Starling
Download Wikimedia's SVN (browse)

Special:ExpandTemplates provides an input box to enter wikitext, and another one to enter a pagename, and produces expanded wikitext, i.e., templates, parser functions and variables are expanded recursively; variables depending on pagename are expanded based on the supplied pagename. It also shows how the produced wikitext is rendered. This is typically the same as how the original wikitext is rendered, but not always.

In special cases the expanded wikitext changes when expanded again: this may apply e.g. if a template is expanded containing "|" or braces: in this case these characters are treated as plain text in the expansion as applied by ExpandTemplates, and, accordingly, in page rendering. This is used in m:Template:! (talk, backlinks, edit), which 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 substitution.

The table, using Template:texpr (talk, backlinks, edit), allows comparison of the wikitext produced with simple substitution and with ExpandTemplates (recursive substitution). It also shows that "subst" prevents ExpandTemplates from expanding the template. It also demonstrates, using also Template:texpr1 (talk, backlinks, edit)}, that a template call with a parameter separator "|" in the form {{!}} prevents ExpandTemplates from expanding the template.

Note for old preprocessor:

Except when the template call is in a parser function call, because that causes an extra pass through the expansion process (compare m:Help:Recursive_conversion_of_wikitext#Parser_functions). By comparison, there is no such extra pass when the template call is in another template call, as the example with Template:1x (talk, backlinks, edit) shows. In the case of this extra pass a parameter separator "|" in the form {{!{{!}}}} can be used, etc.

wikitext wikitext produced ordinary rendering wikitext produced by ExpandTemplates rendering
{{texpr|abc}} 12abcend 12abcend 12abcend
{{subst:texpr|abc}} {{#expr:3*4}}abcend 12abcend {{subst:texpr|abc}} The rendering shown by ExpandTemplates is like the wikitext; for the normal result see on the left.
{{texpr{{!}}abc}} {{texpr|abc}} {{texpr|abc}} 12abcend
{{#if:x|{{texpr{{!}}abc}}}} {{texpr|abc}} {{texpr|abc}} {{texpr|abc}}
{{1x|{{texpr{{!}}abc}}}} {{texpr|abc}} {{texpr|abc}} 12abcend
{{texpr1}} {{texpr|abc}} {{texpr|abc}} 12abcend
{{#if:x|{{texpr{{!{{!}}}}abc}}}} {{texpr{{!|}}abc}} {{texpr{{!|}}abc}} 12abcend

Contents

[edit] Remove comments

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: Unrecognised punctuation character ""

0

1

[edit] Input text in URL

View this page using:
old preprocessor
new preprocessor
diff

The wikitext serving as input can also be put in the URL, e.g., using urlencode: {{fullurl:special:ExpandTemplates|input={{urlencode:{{texpr{{!}}abc}}}}}} giving http://www.mediawiki.org/w/index.php?title=Special:ExpandTemplates&input=%7B%7Btexpr%7Cabc%7D%7D.

"{{!}}", using Template:! (talk, backlinks, edit), 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. http://www.mediawiki.org/w/index.php?title=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}}.

[edit] Installation

  1. Download the files from SVN and place them to extensions/ExpandTemplates
    svn co http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/ExpandTemplates
  2. Make sure you have ExtensionFunctions.php - if not, download it from SVN
  3. Add the following at the end of your LocalSettings.php:
require_once("$IP/extensions/ExpandTemplates/ExpandTemplates.php");

[edit] See also

This extension is being used on one or more of Wikimedia's wikis. It means that the extension is stable and works well enough to be used by such high traffic websites. A full list of the extensions installed on a particular wiki is produced by Special:Version on that wiki.

Personal tools