Extension:TemplateData/zh

TemplateData扩展引入了标记和API，它们共同允许编辑者指定应如何调用模板. 此信息作为最终用户的格式良好的表提供，并作为JSON API提供，它使其他系统（例如VisualEditor）能够构建用于处理模板及其参数的接口. 有关深入的帮助，请参阅.

编辑数据
TemplateData扩展会在模板页面的维基文本中定义标签（当然也可以是从其他页面嵌入包含的），从而实现效果. 该扩展在设计上是兼容一种很多wiki都有的常见布局的，即嵌入包含一个单独的文档页面（但不是必需的）. 的内容必须是有效的JSON，其格式如下所示，注意所有的描述必须是纯文本（模板数据中不接受维基文本）.

页面包含块时，TemplateData会在页面保存时，做出以下检查：


 * 1) 内容必须是有效的JSON；
 * 2) JSON结构中的每一项必须是下文指定的预设类型（例如对象、数组或原始类型）；
 * 3) 对于那些具有指定的可能的值的列表（例如 ）的项，JSON对象中的值必须与这些值中的某一种相匹配.

如果这些检查中的任何一项失败，服务器将不允许用户保存，并且将在编辑页面的上方显示错误消息.

对于自动值的像，bug 2700使得 无法在任何添加到（包括和标签）的模板中生效. TemplateData在这种情况下不会阻止保存，但结果的维基文本在使用模板时仍然不被正确解析.

注意不能使用 替代.

格式
以下内容是JSON数据的视觉化版本，正如包含在标签的模板页面定义的. 格式规定可参考TemplateData. 有关最新版本，请参考Specification.md.

TemplateData对象
模板数据对象是一个“JSON根元素”，其被附于在模板页上的元素中.

InterfaceText (string or object)
A free-form plain-text string (i.e. no wikitext or HTML) in the content-language of the wiki, or an object containing those strings keyed by language code.

The default value for all InterfaceText fields is.

字符串类型
If it is a string, it must contain a non-localized string in the content language of the local wiki.

Object type
If it is an object, the object must have this mapping:

示例
An example TemplateData structure as specified within wikitext on a template page.

Below is how the above example would be displayed on the template page:

{   "description": "Label unsigned comments in a conversation.", "params": { "user": { "label": "User's name", "type": "wiki-user-name", "required": true, "description": "User name of person who forgot to sign their comment.", "aliases": ["1"] },       "date": { "label": "Date", "suggested": true, "description": { "en": "Timestamp of when the comment was posted, in YYYY-MM-DD format." },           "aliases": ["2"], "autovalue": "" },       "year": { "label": "Year", "type": "number" },       "month": { "label": "Month", "inherits": "year" },       "day": { "label": "Day", "inherits": "year" },       "comment": { "required": false }   },    "sets": [ {           "label": "Date", "params": ["year", "month", "day"] }   ],    "maps": { "ExampleConsumer": { "foo": "user", "bar": ["year", "month", "day"], "quux": [ "date", ["day", "month"], ["month", "year"], "year" ]       }    } }

API
See the generated help at Special:ApiHelp/templatedata. A sample invocation (formatted for human readability) is:

This returns the template data for Template:Cite web. The  block for this template is on its transcluded template documentation page, en:Template:Cite web/doc.

Additions and changes to JSON as received from the API
The JSON structure provided in response to an API HTTP get request is significantly different than that which is defined in the  block. In response to the API request, extension TemplateData makes the following changes to the JSON object:


 * Adds two wrapping objects:
 * An overall Pages Object containing one or more Page Objects
 * An object with a numeric key: a Page Object
 * Additions/changes to the actual TemplateData Object
 * Add the  key with the name of the page from which the data was requested (e.g. "Template:Cite web").
 * 添加 键
 * Change all TemplateText occurrences that are just strings to objects with a single key for the current wiki's Wikipedia language code
 * 删除所有 键.
 * Add all properties from parameter from which an inheritance is defined which are not superseded by explicitly defined keys in the inheritor's Parameter Object.
 * Add default values for all keys in each Parameter Object which have not been explicitly defined or inherited.

Example of JSON object provided by API
The response to an API request for the TemplateData structure in the Example section (above) can be seen:
 * HTML格式：https://www.mediawiki.org/w/api.php?action=templatedata&titles=Extension:TemplateData&format=jsonfm
 * 由API提供：https://www.mediawiki.org/w/api.php?action=templatedata&titles=Extension:TemplateData

参见

 * – user interface for selecting and inserting templates' wikitext, based on their TemplateData.
 * GitHub: jeblad/TemplateData – alternate version with some Lua functionality.
 * Translatable template.