Extension:TemplateData

The TemplateData extension introduces a  tag and an API which together allow editors to specify how templates should be invoked. This information is available as a nicely-formatted table for end-users, and as a JSON API, which enables other systems (e.g. VisualEditor) to build interfaces for working with templates and their parameters.

Defining a TemplateData block
The TemplateData extension works through having a  tag defined in the wikitext of a template page (optionally it can be transcluded into the template page as well). This is designed to be compatible with the common layout many wikis have of transcluding template documentation from a separate page (but is not required). This content of the  tag is parsed as JSON using the format described below; note that all descriptions must be in plain text (no wikitext is accepted inside the JSON).

When a  block is included on a page, the TemplateData extension checks the JSON object prior to the page being saved. The checks performed are If any of these checks fails, the page will not be permitted to be saved and an error message will be displayed above the edit box.
 * 1) the JSON object must be properly formatted JSON;
 * 2) each item within the JSON structure must be of the type (object, array, and primitives) specified below; and
 * 3) For those items which have a specified list of possible values (e.g. params.yourParameterName.type), the value in the JSON object must match one of those values.

For autovalue items, bug 2700 prevents subst: from working in any template that is added inside a, including and tags. The TemplateData block will save correctly, but the resulting wikitext will not be parsed correctly when the template is used.

Visualised TemplateData JSON object on a template page
The below is a visualised version of the JSON data as defined on the template page enclosed by   tags. The formal specification is available in the TemplateData repository. Please refer to Specification.md for the latest version.

Example of how TemplateData is displayed on the template page
{	"description": "Label unsigned comments in a conversation.", "params": { "user": { "label": "Username", "type": "wiki-user-name", "required": true, "description": "User name of person who forgot to sign their comment.", "aliases": ["1"] },		"date": { "label": "Date", "description": { "en": "Timestamp of when the comment was posted, in YYYY-MM-DD format." },			"aliases": ["2"], "suggested": true },		"year": { "label": "Year", "type": "number" },		"month": { "label": "Month", "inherits": "year" },		"day": { "label": "Day", "inherits": "year" },		"comment": { "required": false }	},	"sets": [ {			"label": "Date", "params": ["year", "month", "day"] }	] }

Using the TemplateData API
See the automatically formatted help at https://en.wikipedia.org/w/api.php?action=help&modules=templatedata. An example of a working call is https://en.wikipedia.org/w/api.php?action=templatedata&titles=Template:Cite%20web (for Template:Cite web). You can also see a formatted version to better understand the structure.

Additions and changes to JSON as received from the API
The JSON structure provided in response to an API HTTP get request is significantly different than that which is defined in the. In response to the API request, the MediaWiki software makes the following changes to the JSON object:
 * Adds two wrapping objects:
 * An overall Pages Object containing one or more Page Objects
 * An object with a numeric key: a Page Object


 * Additions/changes to the actual TemplateData Object
 * Add the  key with the name of the page from which the data was requested (e.g. "Template:Cite web").
 * Add the  key and array using the order in which the parameters occur in the   key within the TemplateData Object on the template page.
 * Add the  key
 * Change all TemplateText occurrences that are just strings to objects with a single key for the current wiki's Wikipedia language code
 * Remove all  keys.
 * Add all properties from parameter from which an inheritance is defined which are not superseded by explicitly defined keys in the inheritor's Parameter Object.
 * Add default values for all keys in each Parameter Object which have not been explicitly defined or inherited.

Example of JSON object provided by API
The response to an API request for the TemplateData structure in the Example of TemplateData structure WikiText on template page section (above) can be seen:
 * In HTML format: https://www.mediawiki.org/w/api.php?action=templatedata&titles=Extension:TemplateData&format=jsonfm
 * As delivered by the API: https://www.mediawiki.org/w/api.php?action=templatedata&titles=Extension:TemplateData

Live Example

 * Template:Cite web:
 * source template page
 * is transcluded from the template documentation page
 * API response in HTML format: https://en.wikipedia.org/w/api.php?action=templatedata&titles=Template:Cite%20web&format=jsonfm
 * Actual API response: https://en.wikipedia.org/w/api.php?action=templatedata&titles=Template:cite%20web