VisualEditor/TemplateData tutorial

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search

As explained here, TemplateData is a way of storing information about a template - such as parameter names, or a description of the template - so that the VisualEditor can retrieve it and populate the template editor with it. TemplateData is controlled by MediaWiki's TemplateData extension, which allows users to write small bits of structured data to a template page, or to be transcluded into that template page (such as on the standard documentation page). Once a template has this structured data, it can be displayed properly in the VisualEditor. While this may sound complex, it's actually very easy.

How to use TemplateData[edit]

The structure of TemplateData[edit]

TemplateData's structure is based around the "JSON" standard, and is fairly simple. The first thing to do is to type out a pair of <templatedata> tags, anywhere on the template's /doc subpage, like so:

<templatedata>
TemplateData goes here
</templatedata>

This tells the software that everything between the two tags is TemplateData, and should be referenced when the template is used. The TemplateData itself follows a standard layout; let's say that you have a template called "Commons" for linking to a commons category about an article. It has one parameter, and that is the name of the commons category. The TemplateData would look something like:

<templatedata>
{
        "description": "A template for linking to a commons category about an article",
        "params": {
                "1": {
                        "label": "Commons category",
                        "description": "The commons category you want to link to.",
                        "type": "string",
                        "required": true
                }
}
</templatedata>

This would display, in the template, like so:

A template for linking to a commons category about an article

Template parameters

ParameterDescriptionTypeStatus
Commons categoryCategory

The commons category you want to link to.

Stringrequired

What it all means[edit]

So let's break that down. The first tag is a "description", which is fairly self-explanatory; it describes what the template does. There is then a "params" tag, which indicates that subsequent sections cover each parameter in the template.

Within each subsection, the first tag is the name of the template parameter within the template - if the parameter is simply called "1", this tag would be "1". Next we have "label", in which you put a human-readable title for the parameter that will be displayed within the template editor. We then have "description" - this time a description of the parameter, not of the template as a whole. After that we have "type", which controls how the template editor will interpret that parameter. This can be "string" (a set of characters, like this sentence!), "number" (a set of digits), "string/wiki-user-name" or "string/wiki-page-name". We then have "required", which can be set to either true or false; this simply controls whether filling out the parameter is mandatory for that template.

If you have multiple parameters, just repeat each section (starting from the "Category" tag) and fill it out as you see fit. Once you're done, hit "save". If you've made errors, it will not let you save - which we appreciate is disruptive, but means you can't break anything. Should you run into errors, explain on the feedback page what you were trying to do, and we'll be happy to help.

Limitations and questions[edit]