Help:TemplateData
Please see Best practices. |
TemplateData is a way of storing information about a template and its parameters, so that the visual editor can retrieve and display it in its template editor, thus making it easier to edit pages using that template. It is also used elsewhere in scripts, tools, and gadgets.
TemplateData syntax allows users to write small bits of structured data to a template page, or to be transcluded into that template page (such as on a template documentation page). Once a template has this structured data, it can be displayed properly in the visual editor. While this may sound complex, it's actually very easy.
VisualEditor, the visual interface to edit wiki pages, includes a tool to easily edit templates. However, in order for that tool to be most useful, it needs to be able to pull information about the template; mostly, it needs a list of possible parameters for the template, and their characteristics. This is done using Extension:TemplateData .
Best practices
Please consider :
- adding a section heading and
{{TemplateData header}}
above template data as well as a section heading such as==Usage==
==Parameters==
==Template data==
- When editing in Visual Editor, see Help:Visual editor#Headings to add section headings and Help:Visual editor#Adding a template to add a template.
- putting the templatedata in the document subpage
- only marking items "Suggested", not "Required", as both will show parameter boxes when using the Visual editor tool
- marking special-purpose parameters that you do expect people most of the time as "Optional"
- attempting to match the case of parameters in the template although it appears that named parameters to be either initial cap or all lower case
Other considerations
- Template data accepts both named and unnamed parameters
- When initially creating Template Data, adding the suggested options may give you duplicate options
- Templates redirects (such as
{{@}}
) accept the same parameters as template which they redirect - Having redirects for various reasonable capitalization options is encouraged
TemplateData editor
There is a built-in tool for editing TemplateData in a simpler way.
To use the TemplateData editor, go to the template's page (or its documentation subpage) and press the "Edit" button. That will give you a button that says "Manage TemplateData", just above the open editing window:
Description and parameters
description | description>"description" </>, which describes what the template does.
|
"description": " A template for linking to a commons category",
|
---|---|---|
format | format>"format" </>, which describes how the template's wikitext representation should be laid out.
This can be set to the standard formats of <tvar|inline> If the parameter is set to <tvar|inline>
{{Foo | bar = baz | qux = quux }} |
"format": inline
|
params |
There is then a <tvar|params> All the parameters that follow are included in the <tvar|param> |
"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 <tvar|1> If the parameter is "unnamed", meaning it's just a number like <tvar|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
}
| |
label | label>"label" </>, in which you put a human-readable title for the parameter that will be displayed within the template editor.
|
"label": " Commons category",
|
description | description>"description" </>: this time, it's a description of the parameter, not of the template as a whole.
|
"description": " The commons category you want to link to.",
|
default |
Next is <tvar|default> You can ignore this parameter if there's no default. |
"default": "Category:CommonsRoot",
|
type |
After that we have <tvar|type>
a set of characters, like this sentence;
a set of digits;
a set of characters that represents a user's name;
a set of characters that represents a page's title.
a file name.
Other types include |
"type": "string",
|
required |
We then have <tvar|required>
|
"required": true
|
suggested |
There is <tvar|suggested>
|
"suggested": true
|
deprecated |
Finally, there is <tvar|deprecated> This is a status for parameters which should not be used any more, but still exist for the time being. This could be because uses of the template are being moved from one set of parameters to another. If you don't specify, it will be assumed to be <tvar|false> |
"deprecated": "Please use 'publicationDate' instead."
|
Once you're done, hit "save". 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, explain on the feedback page what you were trying to do, and we will be happy to help.
Note that each bit of information is enclosed in quotation marks (except for <tvar|true>true
</> and <tvar|false>false
</>), and separated from the next bit by a comma (unless it's the last one).
Custom formats
When editing custom format strings in the TemplateData editor you can either type \n
or press the enter key to represent a newline; in either case it will display as ↵
in the entry field.
Objective | Format string | Output |
---|---|---|
Inline formatting | {{_|_=_}} inline
|
{{Foo|bar=baz|qux=quux}}{{Bar}} |
Block formatting | {{_\n| _ = _\n}} block
|
{{Foo | bar = baz | qux = quux }}{{Bar }} |
No space before the parameter name, each template on its own line |
\n{{_\n|_ = _\n}}\n
|
{{Foo |bar = baz |qux = quux }} {{Bar }} |
Indent each parameter | {{_\n |_ = _\n}}
|
{{Foo |bar = baz |qux = quux }}{{Bar }} |
Align all parameter names to a given length | {{_\n|_______________ = _\n}}\n |
{{Foo |bar = baz |qux = quux |veryverylongparameter = bat }} {{Bar }} |
Pipe characters at the end of the previous line | {{_|\n _______________ = _}}
|
{{Foo| bar = baz| qux = quux}}{{Bar}} |
Inline style with more spaces, must be at start of line | \n{{_ | _ = _}}
|
{{Foo | bar = baz | qux = quux}} {{Bar }} |
Template at the start of a line, indent-aligned parameters, pipe beforehand | \n{{_ |\n _______________ = _}}
|
{{Foo | bar = baz | qux = quux}} {{Bar}} |
Parameter aliases
Some templates allow a same parameter to have different names.
For example, <tvar|1></>{{Commons|category=Apples}}<tvar|2>
</> could also be written as <tvar|3></>{{Commons|Apples}}<tvar|4>
</> or <tvar|5></>{{Commons|link=Apples}}<tvar|6>
</>.
To add this information to TemplateData, you simply need to add the aliases to the parameter's information:
"params": {
" category": {
...
"aliases": ["1", " link"]
}
Auto value
You can specify an "autovalue" for a parameter. When users add the template to a page, this value will be added automatically. For example, many clean-up templates need to have the date added; if you specify an autovalue for the template's date parameter, then the date will be filled in automatically.
To add this information to TemplateData, simply add the autovalue to the parameter's information.
You will probably want to use the prefix subst:
with the values to make them stick:
"params": {
" date": {
...
"autovalue": "{{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}"
}
Multiple parameters
If you have multiple parameters, 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 so:
"params": {
"1": {
...
}, // notice the comma here
"2": {
...
}, // and here
"3": {
...
}
}
Similar parameters
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.
"params": {
" topic1": {
"label": " Topic",
"description": " A topic mentioned on this disambiguation page",
"type": "string"
},
" topic2": {
"inherits": " topic1"
},
" topic3": {
"inherits": " topic1"
},
}
Blank boilerplate
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>
Template directory
See Category:Templates with TemplateData for a list of templates that had had Template Data added as well as the category.
Related
- TBD