Manual:Parser functions
From MediaWiki.org
| Tag Extensions | Parser Functions | Hooks | Special Pages | Skins | Magic Words |
Contents |
[edit] Parser functions
Parser functions, available since 1.7, provide a way to integrate much more closely than tag extensions with templates and other parser features. There is an embryonic parser function feature in 1.6, but using it is not recommmended, due to the differences in API and syntax. Seems to work well in 1.6.8 with the suggested mods as per ParserFunctions.
Compare with tag hook extensions, which are expected to take unprocessed text and return HTML. There is virtually no integration with the rest of the parser; for example, the output of an extension cannot be used as a template parameter. Expanding templates within extension tags is possible, but must be done manually—an error-prone process, which changes from version to version.
The typical syntax for a parser function is:
{{ #functionname: param1 | param2 | param3 }}
Creating a parser function is slightly more complicated than creating a tag hook, because the function name must be a "magic word"—a kind of keyword that supports aliases and localisation.
[edit] Example parser function extension
Below is an example of an extension that creates a parser function.
<?php # Define a setup function $wgHooks['ParserFirstCallInit'][] = 'efExampleParserFunction_Setup'; # Add a hook to initialise the magic word $wgHooks['LanguageGetMagic'][] = 'efExampleParserFunction_Magic'; function efExampleParserFunction_Setup( $parser ) { # Set a function hook associating the "example" magic word with our function $parser->setFunctionHook( 'example', 'efExampleParserFunction_Render' ); return true; } function efExampleParserFunction_Magic( &$magicWords, $langCode ) { # Add the magic word # The first array element is whether to be case sensitive, in this case (0) it is not case sensitive, 1 would be sensitive # All remaining elements are synonyms for our parser function $magicWords['example'] = array( 0, 'example' ); # unless we return true, other parser functions extensions won't get loaded. return true; } function efExampleParserFunction_Render( $parser, $param1 = '', $param2 = '' ) { # The parser function itself # The input parameters are wikitext with templates expanded # The output should be wikitext too $output = "param1 is $param1 and param2 is $param2"; return $output; }
With this extension enabled,
* {{#example:hello | hi }}
produces:
- param1 is hello and param2 is hi
[edit] Caching
As with tag extensions, $parser->disableCache() may be used to disable the cache for dynamic extensions.
[edit] Parser interface
[edit] Controlling the parsing of output
To have the wikitext returned by your parser function be fully parsed (including expansion of templates), set the noparse option to false when returning:
return array($output, 'noparse' => false);
It seems the default value for noparse changed from false to true, at least in some situations, sometime around version 1.12.
Conversely, to have your parser function return HTML that remains unparsed, rather than returning wikitext, use this:
return array($output, 'noparse' => true, 'isHTML' => true);
However,
This is {{#example:hello | hi }} a test.
will produce something like this:
This is
param1 is hello and param2 is hi a test.
This happens due to a hardcoded "\n\n" that is prepended to the HTML output of parser functions. To avoid that and make sure the HTML code is rendered inline to the surrounding text, you can use this:
return $parser->insertStripItem( $output, $parser->mStripState );
[edit] Naming
By default, MW adds a hash character (number sign, "#") to the name of each parser function. To suppress that addition (and obtain a parser function with no "#" prefix), include the SFH_NO_HASH constant in the optional flags argument to setFunctionHook, as described below.
When choosing a name without a hash prefix, note that transclusion of a page with a name starting with that function name followed by a colon is no longer possible. In particular, avoid function names equal to a namespace name. In the case that interwiki transclusion [1] is enabled, also avoid function names equal to an interwiki prefix.
[edit] The setFunctionHook hook
For more details of the interface into the parser, see the documentation for setFunctionHook in includes/Parser.php. Here's a (possibly dated) copy of those comments:
function setFunctionHook( $id, $callback, $flags = 0 )
Parameters:
- string $id - The magic word ID
- mixed $callback - The callback function (and object) to use
- integer $flags - Optional, set it to the SFH_NO_HASH constant to call the function without "#".
Return value: The old callback function for this name, if any
Create a function, e.g., {{#sum:1|2|3}}. The callback function should have the form:
function myParserFunction( $parser, $arg1, $arg2, $arg3 ) { ... }
The callback may either return the text result of the function, or an array with the text in element 0, and a number of flags in the other elements. The names of the flags are specified in the keys. Valid flags are:
- found
- The text returned is valid, stop processing the template. This is on by default.
- nowiki
- Wiki markup in the return value should be escaped
- noparse
- Unsafe HTML tags should not be stripped, etc.
- noargs
- Don't replace triple-brace arguments in the return value
- isHTML
- The returned text is HTML, armour it against wikitext transformation
[edit] See also
- Manual:Extensions
- Manual:Tag extensions
- Manual:Magic words
- Manual:Parser.php
- Extensions FAQ
- The ParserFunctions extension is a well-known collection of parser functions.