Help:TemplateData/zh

 是一个用于存储wikitext模板及其参数信息的扩展，可以方便可视化编辑器在其模板编辑界面中获取显示，使得用户可以在页面上添加及删除模板.

自2019年4月起，TemplateData是所有維基媒體下Wiki上所有用戶添加模板時的默認用戶體驗的一部分，包括未註冊的用戶. 默認的代碼和可視化編輯器都以一種方式將其納入其中. 腳本、工具和小工具也在其他地方使用. 如果你自己运营维基站点，那么你需要给它安装.

Adding TemplateData to a template essentially consists of inserting a small block of JSON code (explained below) on the template's documentation page. Whilst this may sound complicated, it's rather simple. There are two main ways you can do this, either manually or by using the TemplateData editor, a graphical user interface located on the "Edit"/"Edit source" pages of templates on wikis with the TemplateData extension installed. The JSON contains a list of parameters with info about each parameter of the template as well as the template as a whole. These are outlined below.

Once a template has TemplateData, this information will show in the user interface when a user adds the template, sometimes in place of original info or sometimes as additional info. In some cases, it may change how the editor behaves when editing or inserting the template. This can make it much easier for users to identify what they need to do with a template when adding it.

歷史
TemplateData was originally developed in early 2013 with in mind, which was in MediaWiki testing stages at the time. VisualEditor is the main visual editing interface for Wikimedia wikis, and TemplateData allowed it to have a more useful interface when adding templates. It was officially supported by VisualEditor immediately. As VisualEditor was rolled out to the various major Wikimedia wikis as a default feature from 2013 through 2016, TemplateData became a part of them as well.

In December 2016, the VisualEditor interface was made available for editing source code as a beta feature. This was termed the 2017 wikitext editor. This brought the features of TemplateData to source editing for the first time.

In March 2018, Sam Wilson developed, a GUI template editor extension for the default Wikimedia source editor, (otherwise known as the 2010 wikitext editor), designed to make the process of adding templates easier by using information from TemplateData. In April 2019, TemplateWizard was made a default part of the 2010 wikitext editor on all Wikimedia wikis. This meant that TemplateData was now a default part of both the visual editor and the source editor on Wikimedia wikis.

添加或编辑模板數據
To add or edit TemplateData, first navigate to a template's page, located at "/Template:Templatename".

在添加或编辑之前
There are two things you should check before adding or editing TemplateData, in the following order:


 * 1) whether the template has a documentation subpage
 * 1) whether the template already has TemplateData and whether this is located on the template page or the documentation subpage.

Checking for the documentation subpage

The first thing to do is identify whether the template has a subpage for its documentation or not.

There are two main ways templates on most Wikimedia wikis store their usage notes and other data that shouldn't be included in the actual template itself, like categories the template should be contained in:

This is a page located at the template's name followed by "/doc". In this case, on the main template page after the template source code, there is only the text " " between tags. Here, the Documentation template transcludes all the /doc page's content onto the template page. This makes the template's source code look much cleaner. In this case, it is contained after " ", which is between tags.
 * The vast majority use a documentation subpage.
 * A small minority keep the information on the template's main page, after its source code.

You can quickly identify whether a template has a /doc page by checking if the " Template documentation" (or similar) heading has links like "[view] [edit] [history] [purge]" after it. This may look slightly different on different wikis. Rarely, the main template page may still have documentation content even if you see these buttons and there is a /doc page. This will happen if a user has added a custom /doc page title between " " and " " then added content after " ".

A slightly more reliable way to check is by looking for the notice, "The above documentation is transcluded from Template:Templatename/doc" (or similar), in the box at the bottom of the page.

If a template doesn't have a documentation subpage

If a template doesn't have a documentation subpage, you have two options:

After this, you can then add TemplateData to the documentation subpage. This is the preferred option.
 * You can either create it and move its documentation (if it has any) there.
 * Or, you can add or edit TemplateData on the main template page.

Creating the doc subpage and moving the documentation contents

On most Wikimedia wikis, you can only create a page if you are a registered user (and, at least on the English Wikipedia, have been for more than four days and have made at least ten edits).

To do this, click "Edit"/"Edit source" on the main template page, and look for the tags and their contents, usually at the bottom.

It should look something like this:

It might also have TemplateData already. This will look something like:

Select everything after " " and before "", cut it, then delete " ", so you are just left with:

现在，保存页面.

Next, add "/doc" to the URL in the address bar after the template name, and press Enter. Then, click "Create source".

If your wiki has a Documentation subpage notice template, add it at the top. You may also want to add some other templates, like a Purge button or Purge link, if your wiki has it. 查看元维基:帮助:模板文档获取更多信息

Beneath this, paste the contents of the clipboard.

Next, enclose the categories within tags. This is so that they don't have an effect on the doc subpage, only on the template's main page.

This will look like this:

Some wikis (including the English Wikipedia) have a template. Use this if the template can be expected to have a sandbox version, located at "Template:Templatename/sandbox". It will stop the categories from having an effect on the sandbox page.

This will look like this:

After this, click "Publish page".

You can now add or edit the TemplateData on the documentation subpage based on the guidance at.

Adding or editing TemplateData on the main template page

If you do not wish to or are unable to create the /doc page, you can add or edit TemplateData on the main template page. Follow the guidance at to do this.

If a template has a documentation subpage

If a template has a documentation subpage, you should now check whether it already has TemplateData and whether this is located on the template page or the documentation subpage.

Check both the template page and the /doc subpage for the presence of TemplateData documentation; this looks something like this:

{   "description": "", "params": { "1": {           "label": "Example parameter", "description": "Example parameter description", "type": "string" }   } }

Alternatively, the TemplateData extension can perform this check for you. Click "Edit" or "Edit source" on either page.

If the template already has TemplateData on either page, you will see a yellow notice at the top of the page saying either:


 * "Please note: there is already a TemplateData block on the related page "Template:Templatename/doc"", or


 * "Please note: there is already a TemplateData block on the related page "Template:Templatename""

If the template has TemplateData on its documentation subpage

If the template has TemplateData on its documentation subpage, this is the page you should edit. You can do this by clicking the "[edit]" located after the heading " Template documentation" (or similar), or, if you are already on the /doc page, by clicking "Edit" or "Edit source" at the top. Follow the guidance at to add or edit the TemplateData.

If the template has TemplateData on its main page

If the template has TemplateData on its main page, you once again have two options:


 * You can either move to its documentation subpage. This is the preferred option.
 * Or, you can edit it on the main template page.

Moving TemplateData to the documentation subpage

To do this, click "Edit"/"Edit source" on the main template page, and look for the tags, enclosed within  tags. It should look something like this:

Cut only the tags and their contents out of the code, then save the page.

Next, edit the documentation subpage by clicking the "[edit]" located after the heading " Template documentation" (or similar).

Type a heading, like "TemplateData", then beneath it paste the TemplateData.

You can now edit the TemplateData based on the guidance at.

Editing TemplateData on the main template page

If you do not wish to move the TemplateData to the /doc page, you can edit it on the main template page. See for how to edit TemplateData.

If the template does not yet have TemplateData but has a doc subpage

If the template does not yet have TemplateData, you should add it to the documentation subpage.

Do this by clicking the "[edit]" located after the heading " Template documentation" (or similar), or, if you are already on the /doc page, by clicking "Edit" or "Edit source" at the top. Type a heading, like "TemplateData", then add it beneath this heading. The section explains how to add TemplateData.

模板數據編輯方法
This is the simpler way to add or edit TemplateData, possible for inexperienced users.

模板数据编辑器是一个图形化的编辑器，可用来添加或编辑模板数据. It is part of the TemplateData extension. 要使用模板数据编辑器，请前往模板页面（或其文档页），并点按“编辑”或“编辑源代码”按钮.

After clicking "Edit" or "Edit source", you will see a button above the editing area and page title that says "".

點擊該按鈕就可以進入模板數據編輯器. 如果您所在的頁面已經包含了模板數據，則會自動顯示在這裡.

Features in the above window:


 * The top item is "Language". This item allows you to enter text content in multiple different languages, which will show depending on the language setting of the user in Preferences. It will not show depending on the language wiki, as different wikis have entirely different databases of templates. If no language TemplateData exists yet, only the language of the wiki the user is on will be available, but the user can click "Add language" to add more inputs. The language code will show in brackets next to the parameters for which it can be applied to. These include descriptions and labels.
 * The next item is the template description. This is the description of the template as a whole and is shown in various places to the user in the template editors while adding a template. This parameter and its effects are outlined here.
 * The third item is the wikitext formatting of the template. This parameter determines how the wikitext should be laid out when the user clicks "Insert", the two main types being "inline" (on one line) and "block" (on new lines). This parameter and its effects are outlined here.
 * If the user chooses "Custom", they will be able to enter a set of wikitext under "Custom format string", according to rules outlined here, to create a custom layout for the template when the user clicks "Insert".


 * The fourth item is "Template parameters". This contains a list of parameters already defined in the TemplateData, if they exist. If there are parameters stated in the template's source code for which no TemplateData exists yet, you may see a button called "Add suggested parameters". These are extracted from the template's source code by looking for  or  . Clicking "Add suggested parameters" will add these parameters under their names as defined in the source code. This will also show a green notice at the top: "2 new parameters were imported: names of parameters".
 * At the bottom is "Add parameter". This allows you to add a parameter manually with a custom name.

The next stage is clicking any one of the parameters in the list. This will allow you to edit that parameter's info.



All of these features and their effects are outlined at #Within a parameter's name.

When you are finished, click "Apply". This will automatically insert the TemplateData at the bottom of the page before the tag; or, if it already exists, it will update it with the new information without changing its position at all. The TemplateData text will also be selected when you click "Apply".

The TemplateData editor creates a  parameter at the bottom of the TemplateData or updates it if it already exists. This contains the current TemplateData parameters in the order they were displayed in the TemplateData editor. You can change the paramOrder by dragging the parameters in different orders in the TemplateData editor using the three horizontal bars on the left. and its effects are outlined here.

After this, click "Save" to save the revision of the page.

Manual method
The alternative is to add or edit TemplateData manually. You may need or wish to do this in some cases. 模板数据的结构基于JSON标准，但你无需了解JSON即可知道如何编辑模板数据. It follows a few very simple rules and takes only a few dozen possible predefined parameters and values, almost always in a  format.

Adding TemplateData manually

Preconsiderations:


 * If you're adding TemplateData on a template's documentation subpage, you may add it anywhere on the page; check to see if your language's Wikipedia has a preferred location for it. For example, on the English Wikipedia, TemplateData is typically near the bottom of the documentation page; on the German Wikipedia, it is typically at the top.
 * If you're adding TemplateData on a template's main page (which is not preferred; see #Prior checks), you must make sure that it is within the  tags.

First, click "Edit" or "Edit source" on the respective page, then go to the position you are adding the TemplateData and write a heading like "TemplateData".

Many wikis (but not all) have a TemplateData header template, which adds a short notice about the TemplateData. If yours does, add this after the heading.

To start adding the TemplateData, type an opening and closing tag and a pair of curly brackets on new lines, with an empty line between them:

Next, add an indent on the empty line between the two curly brackets, and start adding parameters. Details for the parameters can be found at. Most are optional, but some are highly recommended.

Parameters can be in any order, but the order that will best correlate with what shows in the TemplateData documentation and contribute to ease of use for editors is the following:

Also, make sure the parameters within, for example, the  object are kept within that object, or you will receive an "Unexpected property" error when you try to save.

范例

Here is some example TemplateData for a hypothetical cleanup template. The template would display a notice, and its source code would be programmed to place the page into a dated category based on the month and year entered. The notice could also contain a link to a talk page section, if specified. The TemplateData would look something like this:

The corresponding TemplateData documentation would display as the following:

{	"description": "Use this template to indicate that an article is in need of cleanup.", "format": "inline", "params": { "date": { "label": "Month and year", "description": "The month and year that the template was added", "type": "string", "autovalue": " ", "example": "January 2013", "suggested": true },		"reason": { "aliases": ["1"], "label": "Reason", "description": "The reason the article is in need of cleanup", "type": "string" },		"talk": { "aliases": ["talksection"], "label": "Talk page section", "description": "The section of the talk page containing relevant discussion", "type": "string" }	},	"paramOrder": [ "date", "reason", "talk" ] }

描述和参数
Each parameter for TemplateData is listed below with a description and example. They are also listed at Extension:TemplateData#Format.

Within a parameter's name
Note: if neither "required", "suggested" nor "deprecated" are set as true for a parameter, its status will show as "optional" in the TemplateData documentation.

一旦您完成，点击“保存”即可. 如果您做出了错误，它将不会让您保存（这或许是破坏，但意思是您不能损坏任何东西）. 您应该运行调试错误，在反馈页面介绍您要尝试进行的操作，并且我们将很高兴帮助.

Note that if you are abusing a hack template to dynamically generate TemplateData, it cannot be checked for errors before saving.

请注意：信息的每一部分都被引号封闭（ 和 除外），且用逗号与下一部分分离开（除非这是最后一个）.

Type parameter
接下来是 ，控制可视化模板编辑器如何解读参数. 参数类型可以是： In some cases, template editors have been programmed to modify the user interface for a certain parameter according to this TemplateData value, such as to only allow the user to enter valid values that match the specified type. This parameter does not have any functional effect on the template parameter or its value; it merely controls how template editors see and treat the parameter in editing mode.

How to use

It is used by adding the  parameter, followed by a colon and a space, then adding any of the values listed in the table below in speech marks.

Example:

Effects

As of February 2020, only 5 of the 13 type values have visible effects in VisualEditor and the 2017 wikitext editor, while 8 have visible effects in TemplateWizard.

The effects of certain values are likely to change as the template editors get updates to support them. One such effort for VisualEditor and the 2017 wikitext editor is tracked in the Phabricator task T55613. A similar effort to get TemplateWizard to support the boolean value is tracked in T200664.

The currently known effects are listed as follows.

自定义格式
When editing the  value, you create custom formats by inputting a set of wikitext symbols using some predefined rules.


 * - start of the template
 * - content (e.g., string, integer or parameter). This underscore serves to indicate the minimum length of a value in characters and can be repeated, like . If this length is not reached, it fills the remaining characters with spaces. This can be used to align all equals signs to a specific position after a parameter (if used with   for new lines).
 * - pipe (separates parameters)
 * - equals sign (precedes the value of a parameter)
 * or pressing the enter key - new line (this will display as  in the entry field)
 * - space (can be used with  to indent new lines)
 * - end of the template

The wikitext should at least meet the minimum of, otherwise there will be an invalid format string error.

空白样板
您可以复制下面的空白样本来向一个模板中添加新的模板信息. 只包含最常见的标签.

Syntax error in JSON / Bad JSON format
This error occurs if you attempt to save TemplateData that has certain types of invalid JSON code, such as duplicate keys (parameters) or trailing/missing commas, in VisualEditor or the 2017 wikitext editor (JavaScript-based editors).

"Syntax error in JSON" shows if you attempt to save such code in VisualEditor or the 2017 wikitext editor, while "Bad JSON format" shows if you attempt to open up such code in the TemplateData GUI editor, on template pages.

This error can come up quite frequently while editing a template's TemplateData. The most common mistakes that cause this error include excess commas (such as trailing commas), missing commas and missing quotation marks.


 * Examples of trailing commas:


 * Examples of missing commas:


 * Example of missing quotation marks:

To fix these errors, you can either look for the errors in the code yourself, or you can enter the JSON block into an external JSON validator, such as JSONLint, and it will highlight the problematic commas and keys that should be removed.

Due to a longstanding bug, users using the 2010 wikitext editor are able to save pages that have such invalid JSON (details: ), as the JavaScript parser is less strict and allows invalid JSON.

Required property "paramOrder(number)" not found.
This error occurs if you state a parameter in  that is not stated in. The number in the square brackets refers to the parameter in  that is missing. It refers to its order in the sequence, but it is one less than its actual position, since  is an array; 0 is the first one, 1 is the second, etc.

To fix this, make sure all parameters stated in  are listed in. Alternatively, you can remove the  object to remove this error.

Invalid value for property "paramOrder(number)".
This error occurs if you state a parameter in  that is not stated in. The number in the square brackets refers to the parameter in  that shouldn't be there. It refers to its order in the sequence, but it is one less than its actual position, since  is an array; 0 is the first one, 1 is the second, etc.

To fix this, make sure all parameters stated in  are listed in. Alternatively, you can remove the  object to remove this error.

Property "params.parametername.required" is expected to be of type "boolean".
This error occurs if you put quotation marks around the value of either  or.

These are boolean values, not strings, therefore they require no quotation marks. To fix this, remove any quotation marks around the values  or   for these parameters.

Property "format" is expected to be ...
If the  parameter exists but its value is not ,   or a valid format string, you will see the error message "Property "format" is expected to be "inline", "block", or a valid format string."

To fix this, make sure the value after  equals   or   and that there no spelling mistakes. Alternatively, if it's wikitext, make sure it contains a minimum of  and that there are no mistakes in the syntax that would normally cause a template to fail, such as duplicate equals signs or missing/duplicate curly brackets; see the section #Custom formats for the syntax for custom formats. Alternatively, you can remove the  parameter to remove this error.

Unexpected property "parametername".
This error occurs if you state a parameter that does not exist in TemplateData. This is probably due to spelling mistakes.

It also occurs if you state a parameter in any TemplateData object that is not a parameter of that object. This may be due to spelling mistakes, or you may have written the parameter of one TemplateData object under another object that it doesn't belong to.

For example, you might have written the parameter  under the root TemplateData object instead of inside  :

Alternatively, if the unexpected parameter is under a template parameter inside, you'll see its name after "params.parametername." in the error.

To fix this, make sure there aren't any spelling mistakes in the parameter names, and make sure your parameters are in their correct location. Additionally, make sure you don't state any parameters that don't exist for a TemplateData object. You can check which parameters exist for an object in the section #TemplateData parameters.

Required property "params" not found.
This error occurs if there is no  object in the TemplateData. This is a necessary object, as it contains all the details of each parameter, so to fix this error, make sure it's added and that there are no spelling mistakes. See the section above on how to add the  parameter.

局限及疑问

 * 缺失功能 – 模板数据是一个工具的例子，它提供了很少的功能，希望用户将有助于指导他们想要的功能的开发. 如果您想要请求模板数据的新功能，请l让我们知道.
 * 模板显示延迟 – 将模板数据添加到模板后，在可视化编辑器中打开模板时，元数据应立即可见. 但是，可能需要几个小时的时间才会显示元数据. 您可以通过对模板页面本身（而不是文档子页面）进行空编辑来强制更新. 要执行空白编辑，请打开模板页面进行编辑，并保存页面，“而不进行任何更改，也不添加任何编辑摘要”.
 * 现有问题 – 现有问题（bug）和特性请求可在Wikimedia错误追踪系统找到.

其它工具

 * : A toolbar dialog window for entering template wikitext via an form built from TemplateData.
 * TemplateData Wizard: 一个通过交互式界面生成模板信息的工具.
 * Skeleton TemplateData generator: 一个读取模板源wiki代码的工具，尝试寻找使用的所有参数，并按列出的参数输出纲要文档.
 * JSONLint: 一种允许您验证手动写入的JSON以帮助查找语法中的错误工具.
 * 在此wiki上所有带模板数据的模板列表
 * 使用dewiki template TemplateData模板数据在框中被显示出来，且该模板被添加到有模板信息的分类当中.
 * https://query.wikidata.org/ and Query Helper