Manual:Parser functions

Parser functions
Tag hook extensions 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.

Parser functions, available since 1.7, provide a way to integrate much more closely 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

The typical syntax for a parser function is:

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. Below is an example of an extension that creates a parser function.

With this extension enabled,

*

produces:


 * param1 is hello and param2 is hi

As with tag extensions, $parser->disableCache may be used to disable the cache for dynamic extensions.

To return unparsed HTML output instead of wikitext output using a parser function, use: return array($output, 'noparse' => true, 'isHTML' => true);

Here is the documentation for setFunctionHook, copied from the comments in the code in includes/Parser.php:

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. . The callback function should have the form:

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

The ParserFunctions extension is a well-known collection of parser functions.

Naming
When choosing a name without a hash character (number sign, "#"), 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 is enabled, also avoid function names equal to an interwiki prefix.