Help:TemplateData

From Saintapedia
Jump to navigation Jump to search
Shortcut:
TD
File:TemplateData Editor.png
Screenshot showing where to add template data when edit a template or it's documentation page.

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:TemplateDataMediawiki logo.svg.

Best practices

Please consider :

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>"inline"</> (<tvar|default>Template:Int</>) and <tvar|block>"block"</>, or to a custom format; see below for more details.

If the parameter is set to <tvar|inline>"inline"</> it will create a wikitext representation with no white space, like so:


{{Foo|bar=baz|qux=quux}}


If the parameter is set to <tvar|block>"block"</> it will create a wikitext representation with line breaks and single spaces between each part, like so:

{{Foo
| bar = baz
| qux = quux
}}
"format": inline
params

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

All the parameters that follow are included in the <tvar|param>"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 <tvar|1></>{{{category-link}}}<tvar|2></>, this tag would be <tvar|3></>"category-link"<tvar|4></>.

If the parameter is "unnamed", meaning it's just a number like <tvar|1>{{{1}}}</>, this tag would be <tvar|1b>"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>"default"</>. Some templates have a default value that is used unless you change it. This item tells the user what the default value for this parameter is.

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

"default": "Category:CommonsRoot",
type

After that we have <tvar|type>"type"</>, which controls how the template editor will interpret that parameter. The most common types are:

  • "string":

a set of characters, like this sentence;

  • "number":

a set of digits;

  • "boolean": '0' for false, '1' for true, '' for unknown;
  • "wiki-user-name":

a set of characters that represents a user's name;

  • "wiki-page-name":

a set of characters that represents a page's title.

  • "wiki-file-name":

a file name. Other types include "unknown", "date", "url", "wiki-template-name", "content", "unbalanced-wikitext", "content", and "line".

"type": "string",
required

We then have <tvar|required>"required"</>, which can be set to either <tvar|true>true</> or <tvar|false>false</>.


This simply controls whether filling out the parameter is mandatory for that template. If you don't specify, it will be assumed to be <tvar|false>false</>.

"required": true
suggested

There is <tvar|suggested>"suggested"</>, which can be set to either <tvar|true>true</> or <tvar|false>false</>.


This is a status for parameters which are not 'required' but are recommended to be high value (but not mandatory) for template users. If you don't specify, it will be assumed to be <tvar|false>false</>.

"suggested": true
deprecated

Finally, there is <tvar|deprecated>"deprecated"</>, which can be set to <tvar|true>true</>, <tvar|false>false</>, or a string describing what users should instead do.

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>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.

Examples of formats you can use
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

External links