Help:TemplateData

From Wiktionary
Jump to: navigation, search

TemplateData is a way of keeping information about a template and its parameters, so that VisualEditor can know more about a template and display it in the template editor, hence making it easier to use templates on pages.

TemplateData allows users to keep some important information about a template on the template page (or its documentation page). This information is then used in VisualEditor so that it can show more information about a template to the user.

Structure of TemplateData[change]

TemplateData's structure is based around the JSON standard, and is fairly simple. Note that all descriptions in TemplateData must be in plain text (no wikitext, no links, etc.).

The first thing to do is to type out a pair of <templatedata> tags in the TemplateData section of either the template documentation page, or the template itself, like this:

<templatedata>
{
        ...                            <-- TemplateData content goes here
}
</templatedata>

This tells the software that everything between the two tags is TemplateData, so that other tools like VisualEditor know where to find the TemplateData information.

Example of TemplateData[change]

The descriptions inside TemplateData follow a standard layout. Let's say that you have a template called "Commons" for linking to a Commons category about a topic. It takes one required parameter, which is the name of the category on Commons. The TemplateData would look something like this:

<templatedata>
{
	"description": "A template for linking to a Wikimedia Commons category.",
	"params": {
		"1": {
			"label": "Category",
			"description": "The Commons category to link to.",
			"default": "CommonsRoot",
			"type": "string",
			"required": true
		}
	}
}
</templatedata>

This would display a table in the template, like this:

A template for linking to a Wikimedia Commons category.

Parameter Description Type Default Status
Category 1 The Commons category to link to. string CommonsRoot required

Description and parameters[change]

The first tag is a "description", which describes what the template does.
"description": "A template for linking to a Wikimedia Commons category.",

There is then a "params" tag, which indicates that subsequent sections cover each parameter in the template.

All the parameters that follow are included in the "params" section.

"params": {
	...            <-- parameters go here
}

Within each parameter's subsection, the first tag is the name of the template parameter within the template.

If the parameter has a name, like {{{category-link}}}, this tag would be "category-link".

If the parameter is "unnamed", meaning it's just a number like {{{1}}}, this tag would be "1".

All the bits of information about this parameter are included in the section that starts with the parameter's name.

	"1": {                 <-- name of the parameter
		...            <-- information about the parameter goes here
	}

Next we have "label", in which you put a human-readable title for the parameter that will be displayed within the template editor.
		"label": "Category",

We then have "description": this time, it's a description of the parameter, not of the template as a whole.
		"description": "The Commons category to link to.",

Next is "default", which defines the default value for the parameter.

You can ignore this parameter if there's no default.

		"default": "CommonsRoot",

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": a set of characters that represents a user's name.
  • "string/wiki-page-name": a set of characters that represents a page's title.
		"type": "string",

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.

		"required": true

Once you're done, click the "Save page" button to save your changes. If you've made errors, it will not let you save (which is disruptive, but means you can't break anything). Should you run into errors, the software should give you a brief description about the problem, but if you still do not understand what it means, you can ask other Wiktionarians for help.

Note that each bit of information is enclosed in quotation marks (except for true and false), and separated from the next bit by a comma (unless it's the last one).

Parameter aliases[change]

Some templates allow the same parameter to have different names for them.

For example, {{commonscat|category=Apples}} can also be written as {{commonscat|Apples}} or {{commonscat|link=Apples}}.

To add this information to TemplateData, you just need to add the aliases to the parameter's information, like this:

	"params": {
		"category": {
			...
			"aliases": ["1", "link"]
		}
	}

Multiple parameters[change]

If you have multiple parameters for a template, just repeat each section (starting from the "1" tag) and fill it out as you see fit. Note that if a template has multiple parameters, you need to separate them with a comma in the templatedata, like this:

	"params": {
		"1": {
			...
		},           <-- notice the comma here
		"2": {
			...
		},           <-- and here
		"3": {
			...
		}            <-- but not here (because it is the last one)
	}

Similar parameters[change]

When a template has multiple parameters, sometimes some of them can be of the same kind. In this case, you only need to provide full properties for the first one, and the others can "inherit" their properties from it. For example:

	"params": {
		"topic1": {
			"label": "Topic",
			"description": "A topic mentioned on this disambiguation page.",
			"type": "string"
		},
		"topic2": {
			"inherits": "topic1"
		},
		"topic3": {
			"inherits": "topic1"
		},
	}

Blank boilerplate[change]

You can copy the blank boilerplate below to add new TemplateData to a template. Only the most common tags are included.

<templatedata>
{
	"description": "",
	"params": {
		"1": {
			"label": "",
			"description": "",
			"type": ""
		},
		"2": {
			"label": "",
			"description": "",
			"type": ""
		}
	}
}
</templatedata>

See also[change]