Difference between revisions of "Template:Data"

From Kolmafia
Jump to navigation Jump to search
m (Philmasterplus moved page User:Philmasterplus/Data to Template:Data: Testing complete, ready for use)
Line 1: Line 1:
<includeonly>{{#ifexist:{{{1|}}}|{{User:Philmasterplus/Sandbox|format=#switch:{{{2|}}}|{{{3|}}}}}|{{error|Missing parameter '1' in User:Philmasterplus/Data}}}}</includeonly><noinclude>
+
<includeonly>{{{1|{{{error|Missing parameter '1' for Template:Data}}}{{#ifexist:{{{1|}}}|{{User:Philmasterplus/Sandbox|format=#switch:{{{2|}}}|{{{3|}}}}}|{{error|Page '{{{1|}}}' does not exist}}}}</includeonly><noinclude>
  
Based on the [http://kol.coldfront.net/thekolwiki/index.php/Template:Data Template:Data] page.
+
<templatedata>
 +
{
 +
  "description": "Utility template that extracts a field from a data page.",
 +
  "params": {
 +
    "1": {
 +
      "label": "Page name",
 +
      "description": "Name of the data page to extract the data from.",
 +
      "type": "wiki-page-name",
 +
      "required": true,
 +
      "example": "visit_url"
 +
    },
 +
    "2": {
 +
      "label": "Field name",
 +
      "description": "Name of the field to extract from the source page",
 +
      "type": "line",
 +
      "required": true,
 +
      "example": "function1.parameter1"
 +
    },
 +
    "3": {
 +
      "label": "Default value",
 +
      "description": "Default value if the page does not contain the given field, or the field is empty",
 +
      "type": "string",
 +
      "example": "no_name"
 +
    }
 +
  }
 +
}
 +
</templatedata>
  
==Examples==
+
This template is based on the [http://kol.coldfront.net/thekolwiki/index.php/Template:Data Template:Data] page.
<pre>
+
 
*name: {{User:Philmasterplus/Data|User:Philmasterplus/Sandbox|name}}
+
==What is a Data Page?==
*function1.return_type: {{User:Philmasterplus/Data|User:Philmasterplus/Sandbox|function1.return_type|}}
+
 
*function2.param2: {{User:Philmasterplus/Data|User:Philmasterplus/Sandbox|function2.param2|}}
+
Suppose a page named "XYZ" contains the following wikitext:
</pre>
+
<syntaxhighlight lang="html"><onlyinclude>{{{{{format|DefaultTemplate}}}
 +
|param1=abc
 +
|foo=bar
 +
|some.property=some value
 +
|empty_value=
 +
|{{{1|}}}
 +
}}</onlyinclude></syntaxhighlight>
 +
 
 +
Any page that contains code like this is a _data page_. A data page cannot render itself, but instead feeds its parameters to a rendering template ("DefaultTemplate" in this example) which is responsible for rendering the page.
 +
 
 +
The [[Template:Data]] template can extract individual fields from a data page, like this:
 +
 
 +
{| class="wikitable"
 +
! Wiki markup !! Result
 +
|-
 +
| <pre>{{data|XYZ|param1}}</pre>
 +
| abc
 +
|-
 +
| <pre>{{data|XYZ|foo}}</pre>
 +
| bar
 +
|-
 +
| <pre>{{data|XYZ|some.property}}</pre>
 +
| some value
 +
|-
 +
| <pre>{{data|XYZ|empty_value}}</pre>
 +
|
 +
|-
 +
| <pre>{{data|XYZ|empty_value|default value}}</pre>
 +
| default value
 +
|-
 +
| <pre>{{data|XYZ|value that does not exist|default value}}</pre>
 +
| default value
 +
|}
  
*name: {{User:Philmasterplus/Data|User:Philmasterplus/Sandbox|name}}
 
*function1.return_type: {{User:Philmasterplus/Data|User:Philmasterplus/Sandbox|function1.return_type|}}
 
*function2.param2: {{User:Philmasterplus/Data|User:Philmasterplus/Sandbox|function2.param2|}}
 
 
</noinclude>
 
</noinclude>

Revision as of 21:01, 18 December 2020


Utility template that extracts a field from a data page.

Template parameters

ParameterDescriptionTypeStatus
Page name1

Name of the data page to extract the data from.

Example
visit_url
Pagerequired
Field name2

Name of the field to extract from the source page

Example
function1.parameter1
Linerequired
Default value3

Default value if the page does not contain the given field, or the field is empty

Example
no_name
Stringoptional

This template is based on the Template:Data page.

What is a Data Page?

Suppose a page named "XYZ" contains the following wikitext:

<onlyinclude>{{{{{format|DefaultTemplate}}}
|param1=abc
|foo=bar
|some.property=some value
|empty_value=
|{{{1|}}}
}}</onlyinclude>

Any page that contains code like this is a _data page_. A data page cannot render itself, but instead feeds its parameters to a rendering template ("DefaultTemplate" in this example) which is responsible for rendering the page.

The Template:Data template can extract individual fields from a data page, like this:

Wiki markup Result
{{data|XYZ|param1}}
abc
{{data|XYZ|foo}}
bar
{{data|XYZ|some.property}}
some value
{{data|XYZ|empty_value}}
{{data|XYZ|empty_value|default value}}
default value
{{data|XYZ|value that does not exist|default value}}
default value