Wikipedia:TemplateData/Tutorial: Difference between revisions
Content deleted Content added
KETA031103 (talk | contribs) |
m Reverted edit by 2C0F:FC89:BC:B91:608D:CE37:67FA:9698 (talk) to last version by SurabhiMinocha |
||
(44 intermediate revisions by 32 users not shown) | |||
Line 1:
{{short description|Wikimedia project gadget tutorial}}
{{shortcut|WP:TDATA/T|WP:VE/TDT}}
{{VisualEditor navbox}}
[[Wikipedia:VisualEditor/TemplateData|
== How to use
===The structure of
{{see also|mw:Help:TemplateData|mw:Extension:TemplateData}}
<syntaxhighlight lang="xml">
<templatedata>
<TemplateData>▼
TemplateData goes here
</templatedata>
</TemplateData>▼
</syntaxhighlight>
<small>N.B. The preceding is incorrect code if used as it is and will therefore throw a JSON error if used; see [[#Completing the
This tells the software that everything between the two tags is
On English Wikipedia, you can also use the {{tl|Format TemplateData}} template to generate a more "conventional" presentation of template data suitable for use in [[Wikipedia:Template documentation|Template documentation]]. Instead of the above, write this at the top of the documentation page:
===TemplateData===▼
<syntaxhighlight lang="xml">
The TemplateData is added to the template page itself inside <code><nowiki><noinclude></noinclude></nowiki></code> tags, or anywhere on the template's documentation page if it has one. In some cases, the main template page will be locked to prevent editing by unauthorized users. In that situation, the TemplateData can only be added to the /doc page; the link to that page is at the bottom of the main page.▼
|1=<templatedata>
</templatedata>
|TOC=1
}}
</syntaxhighlight>
===Template data===
The TemplateData is generally placed after the descriptive information about the template, and before the "See also" section.▼
▲The
▲The
'''Note''': You should add <nowiki>{{</nowiki>[[Template:TemplateData header|TemplateData header]]<nowiki>}}</nowiki> directly in front of the <nowiki><templatedata></nowiki> tag. This will categorize the page as TemplateData documentation and allow later organization of templates and their documentations.▼
▲'''Note''': You should add <nowiki>{{</nowiki>[[Template:TemplateData header|TemplateData header]]<nowiki>}}</nowiki> directly in front of the <code><nowiki><templatedata></nowiki></code> tag. This will categorize the page as
===Identifying the parameters===
If the template has no parameters, you can complete its
However, most templates have parameters, such as dates, URLs, article names, images, numbers or strings. Determine which parameters are or may be used in the template. Parameters may be passed by equal signs. For instance, the <code><nowiki>{{cite web}}</nowiki></code> template is passed values to complete a citation, such as <code>url=</code>, <code>title=</code>, <code>
The [[Wikipedia:Template documentation|template documentation]] on the main or sub-page may summarize which parameters can be set on a template, and what each parameter must include. Also, template data must be included in the template documentation page.
▲However, most templates have parameters, such as dates, URLs, article names, images, numbers or strings. Determine which parameters are or may be used in the template. Parameters may be passed by equal signs. For instance, the <code><nowiki>{{cite web}}</nowiki></code> template is passed values to complete a citation, such as <code>url=</code>, <code>title=</code>, <code>accessdate=</code>, ''etc.'' Other parameters may be used by their position instead. In this case, use numbers "<code>1</code>", "<code>2</code>", ''etc.'' instead of names for the parameters. The template documentation on the main or sub-page may summarize which parameters can be set on a template, and what each parameter must include.
===Completing the
Remember,
The first piece of information to fill out is a <code>"description"</code> for the overall template, which is fairly self-explanatory; it briefly describes what the template does and explains where and how the user might want to use it. If the template was created and given some kind of description by someone before you, you may use information that you glean from the top of the main or sub-page of the template, which you can copy and paste here. Place it within quotes and follow it with a comma: <code>"description": "This template generates an information box for Croatian football players",</code>). Note that any wikitext typed anywhere in the
Next, you should create a <code>"params"</code> block, with braces (<code>{</code> and <code>}</code>). Inside this block, you need to create a sub-block for each parameter used by the template, with some of the following entries. Most of it is optional, but the more information you give the easier it will be for others to re-use the template.
:* Create or use the short arbitrary <code>"name"</code> for the parameter that will be read by the Wikipedia template software. If you use or the template has a parameter name containing more than one word, these must always be separated by an underscore: <code>_</code> like this: <code>ocean_size</code> (i.e., parameter names do not contain spaces). Place it between double quotes, follow it with a colon, and create a block with some more single braces <code>{</code> and <code>}</code> like this: <code>"ocean_size": { }</code> or <code>"range_map": { }</code>. Note that many templates like {{tl|Infobox Fabergé egg}} have a variable which is itself called "name", and therefore the JSON code for this parameter would be written <code>"name": { }</code>. The following code now goes within these braces, separated by a comma at the end of each but none after the last, and with no additional braces (Note that the order of these entries is irrelevant: they will be ordered in a consistent pattern when the JSON
:** The <code>"'''label'''"</code> entry is a human-readable title for the parameter that will be displayed within the template editor. Capitalize the first character of the label (since it will be the leftmost value in the resulting table), and put it in quotes, like this: <code>"Ocean size":</code>.
:** Enter the parameter's <code>"'''description'''"</code> (i.e., a description of the specific parameter, not the template as a whole). This may already have been written on the template's main or documentation page, and can be copied and pasted into place. Put this information in quotes. If you wish your description to also display a word or phrase in quotes, you must [[Escape character|escape]] the quotation marks by putting a backslash <code>\</code> directly ''before'' them like this: <code>"This parameter indicates the \"size\" of the ocean"</code>. If you do not provide the backslash directly before the quotation mark, the JSON software will interpret the mark as the end of the parameter block— the backslash tells JSON, "Don't count the quotation mark directly following me— render it as a visible quotation mark and look for the end-of-parameter quotation mark further on". If the word you wish to display in quotes is at the very end of your parameter description, you will just need to type <code>\""</code> like this: <code>"This parameter
:** You can optionally set a flag on the status of the parameter:
:*** <code>"'''required'''"</code> says that filling out the parameter is mandatory for that template. Only set this to <code>true</code> if the value is required for the template, and not setting a value will break the template (like the URL for [[Template:Cite web|Cite web]]). Entries for this flag must be either the word <code>true</code> or <code>false</code>, with no quotation marks.
Line 44 ⟶ 57:
:*** <code>"'''deprecated'''"</code> can be set to flag whether or not this parameter is in regular use – as well as setting to <code>true</code>, you can write a brief description about what users should do instead. Rare.
:** The <code>"'''aliases'''"</code> group lets you list other names for this parameter which have been set to work equally well, and its entry is enclosed by a single bracket, i.e., <code>"aliases": [ "2", "Caption", "imagecaption" ]</code>. An alias is an alternative name for the parameter that the template is willing to accept instead of (not in addition to) the primary name. Aliases are not documented in a separate parameter object.
:* The <code>"'''autovalue'''"</code> entry lets you tell VisualEditor and other tools to pre-fill this parameter with a standard value (in wikitext); this text will show up in the parameter box when users edit, and will be added to the template invocation when saved. This may be useful for cleanup templates to automatically set the date the user adds a template. For example, add <code><nowiki>
:* The <code>"'''default'''"</code> setting lets you show what the template will do if this parameter
:* The <code>"'''example'''"</code> entry allows you to display an example of an entry that a person might make for this variable, written exactly as a person might type it, character-for-character; if the type of entry has already been set with the <code>"'''type'''"</code> parameter (see below), then the example should reflect this (i.e., if the type has been set to "wiki-page-name" then your example should ''not'' include a prefix like "File:" or "Image:"). Be sure to enclose the entire text of the example with quotes. Obviously, this means the actual example may not contain quotation marks itself. If not created by the person writing the
:* The <code>"'''type'''"</code>, which controls how the template editor will interpret that parameter. This can be one of a few values, any of which used must be enclosed in double quotation marks:
{| class="wikitable"
Line 61 ⟶ 74:
|-
|<code>"string"</code>
|Any textual value. May contain line breaks.
|-
|<code>"line"</code>
|Short text field
|-
|<code>"wiki-page-name"</code>
Line 79 ⟶ 92:
|-
|<code>"unbalanced-wikitext"</code>
|Raw wikitext that should not be treated as standalone content because it is unbalanced
|-
|<code>"date"</code>
|A date in [[ISO 8601]] format, e.g. "2014-05-09" or "2014-05-09T16:01:12Z"
|}
:* The <code>"'''suggestedvalues'''"</code> parameter property lets you define a list of parameter values to be shown to VisualEditor users for easy selection. Once the values have been added to TemplateData, the VisualEditor will display them in a combo box (a dropdown into which users can also enter a custom value). The user selects the desired value by clicking on it. For the suggested values to be displayed in the VisualEditor, the parameter’s type must be one of the following: content, line, string, number, unknown or unbalanced wikitext. Adding this parameter property can be done either in JSON or using the TemplateData Editor with no coding required. [[m:WMDE_Technical_Wishes/Suggested_values_for_template_parameters#TemplateData|Both methods are explained here.]]
:* <code>"inherits"</code>. This is a key to another parameter. The current parameter will inherit from that one, with local properties overriding the inherited ones. Very rare. '''Currently, the inclusion of this parameter in a <code><nowiki><templatedata></nowiki></code> sub-block will cause the page to produce a ''Required property "params." not found.'' error and will fail to generate the expected table.'''
<nowiki> </nowiki>Where more than one parameter sub-blocks is passed to the template, you must add a comma after the close brace, <code>},</code>, between the sub-blocks. Do not place a comma after the last sub-block in your set, but do make sure that there is a final close brace, <code>}</code>.
A further option, <code>"format"</code>, determines how the wikitext code for the template will be formatted when it is saved by the VisualEditor.
<syntaxhighlight lang="wikitext">
|
| genre = Comedy
}}
</syntaxhighlight>
This option may be preferable for very complex templates like infoboxes which have multiple parameters. The documentation page indicated the format with the line {{small|"This template prefers inline formatting of parameters."}} or {{small|"This template prefers block formatting of parameters."}}
For more complex parameter formatting options, see [[:mw:Help:TemplateData#Custom_formats]].
==== Save ====
Once you're done, hit "save". If you've made errors, it will not let you save – which can be a little frustrating, as the resulting error message will not tell you where JSON encountered the first code error, but means you
* Is every opening quote (<code>"</code>) matched with a closing quote in the correct place of the code?
* Does a string, such as a description of a parameter, contain a <code>"</code> that is not at the end of the entry? If so, consider replacing it with a <code>'</code>.
Line 103 ⟶ 121:
Manually searching through the program for these errors can be tiresome and difficult. Fortunately, a number of program checking websites for JSON exist which will at least identify the line on which the program first encounters an error. [http://jsonlint.com/] is one of these which seems to work quite well. Simply copy and paste the problematic JSON code into the corresponding box on the website and ask it to check the code— it will not itemize every error in the document, but it ''will'' indicate the ''first'' error it encounters, if any, which should be immensely helpful in correcting your code.
Once you successfully save the page, it may take a few minutes after saving for the
=== Worked example ===
The {{tl|Str left}} template is a simple template used like <code><nowiki>{{Str left|<string>|<count>}}</nowiki></code> to show the first few characters of an input. It has two parameters, neither of which are named (they are only recognised by their position in the template), and both of which are required. Thus the
<syntaxhighlight lang="javascript">
<templatedata>
Line 149 ⟶ 167:
</templatedata>
Note: As explained above, the TemplateData block does not affect how the template works, but it does affect editors' use of the template in the VisualEditor. In the {{tl|Str left}} example above, parameter {{para|2}} is listed as "required", which means that the VisualEditor will require entry of a value, even though the template's actual code does not require parameter {{para|2}} to be present in transclusions of the template. This difference can be confusing to editors.
=== Complete empty template data block ===
You can just copy and paste this to use when creating your own.
<syntaxhighlight lang="javascript">
<templatedata>
{
"description": "
"params": {
"
"label": "x",
"description": "xxx",
Line 192 ⟶ 189:
"type": "string"
},
"
"label": "y",
"description": "yyy",
Line 243 ⟶ 240:
===Shared documentation===
Many templates share the same documentation.
This can be resolved by putting the
===Help===
Line 251 ⟶ 248:
== Examples ==
A template which takes no parameters: {{tl|fixed}}. Note the params must be
{{markup |
<nowiki>
Line 390 ⟶ 387:
== Limitations and questions ==
Template data was previously limited to 65,535 bytes (Phabricator: <s>{{phab|53740}}</s>). This limit could be exceeded for some templates which use many parameters, such as {{tl|Infobox officeholder}}, but the code is now compressed, increasing the limit.
== Tools ==
[[File:TemplateDataEditor.png|thumb|Editor for [[:en:Wikipedia:VisualEditor/TemplateData|
* [[User:NicoV/TemplateDataEditor|TemplateDataEditor]] A user script that makes the process of adding
* [http://tools.wikimedia.pl/~mlazowik/templatedata/?lang=en Yet another template data editor]
* [[User:Salix alba/TDSkell|TemplateData Skeleton]] — Read the source of a template and tries to find all the parameters used and output a skeleton document with all the parameters listed. Javascript toolbox popup.
* [[Module:TemplateDataGenerator]]
* {{tl|Format TemplateData}}
* [[:ru:Модуль:TemplateDataDoc]] allows to create blank copy and usage example from
== External links ==
{{MediaWiki|Extension:TemplateData#Defining a TemplateData block}}
*[http://jsonlint.com/ Jsonlint] a JSON validator to help spot errors in the template data syntax.
*[https://json-validate.com/ Json Validate] – A tool for not just identifying errors and validating JSON data but also converting it into different formats, ensuring data integrity.
[[Category:Wikipedia template help]]
|