Template:Data to Table
Note: parts of or the entire template might not be visible without values provided.
| Main | Documentation | Usage |
Lua module to allow users to create "copy-pastable" data tables based on custom DPL queries, as well as JSON data from DPL queries.
Syntax
- info — The raw output of a DPL query (see usage examples).
- keys — A ;;-separated list of keys to extract from the info. You can use nested levels with // and @@.
- Note that although the page itself must be included in your DPL include statement, it is used as the table's key by default, so there is no need to specify it.
- unquoted (optional) — A ;;-separated list of keys whose values should be output as raw Lua values instead of strings.
- value (optional) — Sets the same value for every entry when no keys are specified.
- sortkey (optional) — Specifies which key to sort the data by, using the page name by default. Use INPUT to preserve the original order.
- subtable (optional) — Splits values into subtables.
- block (optional) — Display the results using block formatting.
- copy (optional) — Enter your header here. Adds a button to copy the generated Lua table to your clipboard at the top.
- cleanTT (optional) — Auto-parse out any {{tt}} format in the output.
JSON Mode
- json — Set to 1 to output JSON instead of a Lua table.
- group1, group2, etc. — The name of a JSON group.
- group1_info, group2_info, etc. — The DPL output used to populate the corresponding group.
- Groups may be numbered sequentially up to group10.
- Empty groups are automatically omitted from the generated JSON.
- JSON mode uses the built-in copy button provided by the syntax highlighting extension.
When json is set to 1, the module supports both simple JSON arrays and grouped JSON objects.
Examples
Lua Table
A basic DPL query can be converted into a Lua data table:
{{Data to Table
|keys=number;;quality;;galaxy
|unquoted=number
|info={{#DPL:
|namespace=
|category=Fish
|uses=Template:Creature Infobox
|include={Creature Infobox}:%PAGE%,{Creature Infobox}:number,{Creature Infobox}:quality,{Creature Infobox}:distribution
|format=,,,
|secseparators=page==,;;number==,,;;quality==,,;;galaxy==,,$$$
}}
}}
This produces output in the following format: Template:Sh
Constant Values
value can be used when every entry should have the same value:
{{Data to Table
|value=day
|info={{#DPL:
|namespace=File
|category=Icons to Invert
|format=,page==%PAGE%$$$,,
}}
}}
This produces: Template:Sh
If the value should be output as a raw Lua value rather than a string, include value in unquoted.
Simple JSON Array
JSON mode can also be used without groups:
{{Data to Table
|json=1
|info={{#DPL:
|namespace=File
|category=Icons to Invert
|format=,page==%PAGE%$$$,,
}}
}}
This produces: Template:Sh
Grouped JSON
Multiple DPL queries can be combined into a single JSON object using groups:
{{Data to Table
|json=1
|group1=day
|group1_info={{#DPL:
|namespace=File
|category=Icons to Invert
|format=,page==%PAGE%$$$,,
}}
|group2=night
|group2_info={{#DPL:
|namespace=File
|category=Icons to Invert Night
|format=,page==%PAGE%$$$,,
}}
}}
This produces:
Groups with no results are automatically omitted. For example, if the night category is empty, the output will simply be:
Multiple Groups
Additional groups can be added by continuing the numbering:
|group1=day |group1_info=... |group2=night |group2_info=... |group3=special |group3_info=...
Up to group10 / group10_info are supported.
DPL Formatting for JSON
When using JSON mode, each DPL query should output an entry using the page==...$$$ format.
For example:
|format=,page==%PAGE%$$$,,
The page value is used as the value in the JSON array.
For file categories, the resulting JSON can therefore contain the full filename, such as:
rather than only the page title or the filename without its extension.
Notes
- JSON output is sorted alphabetically by page name.
- Lua output is sorted according to sortkey, which defaults to the page name.
- JSON mode does not use Template:M; it uses the copy button provided by the syntax highlighting extension.
- Normal Lua output can still use copy to add the custom copy button.
- The module automatically omits empty JSON groups.
- The module is intended to be used with DPL output, rather than being tied to any particular data source.
Template Data
Converts custom DPL output into copy-pastable Lua tables or JSON data.
| Parameter | Description | Type | Status | |
|---|---|---|---|---|
| DPL output | info | The raw output of a DPL query. | String | required |
| Keys | keys | A ;;-separated list of keys to extract from the DPL output. Nested levels can be created with // and @@. | String | optional |
| Unquoted keys | unquoted | A ;;-separated list of keys whose values should be output as raw Lua values instead of strings. | String | optional |
| Constant value | value | Sets the same value for every entry when no keys are specified. | String | optional |
| Sort key | sortkey | The key used to sort the Lua output. Defaults to the page name. Use INPUT to preserve the original order. | String | optional |
| Subtable separator | subtable | Splits values into subtables using the specified separator. | String | optional |
| Block formatting | block | Display the generated Lua table using block formatting. | Boolean | optional |
| Copy button | copy | Adds a copy button to the generated Lua table using the supplied header text. | String | optional |
| Clean Template:Tt | cleanTT | Automatically parses Template:Tt formatting from the DPL output. | Boolean | optional |
| JSON mode | json | Set to 1 to output JSON instead of a Lua table. | Boolean | optional |
| JSON group 1 | group1 | The name of the first JSON group. | String | optional |
| JSON group 1 data | group1_info | The DPL output used to populate JSON group 1. | String | optional |
| JSON group 2 | group2 | The name of the second JSON group. | String | optional |
| JSON group 2 data | group2_info | The DPL output used to populate JSON group 2. | String | optional |
| JSON group 3 | group3 | The name of the third JSON group. | String | optional |
| JSON group 3 data | group3_info | The DPL output used to populate JSON group 3. | String | optional |
| JSON group 4 | group4 | The name of the fourth JSON group. | String | optional |
| JSON group 4 data | group4_info | The DPL output used to populate JSON group 4. | String | optional |
| JSON group 5 | group5 | The name of the fifth JSON group. | String | optional |
| JSON group 5 data | group5_info | The DPL output used to populate JSON group 5. | String | optional |
| JSON group 6 | group6 | The name of the sixth JSON group. | String | optional |
| JSON group 6 data | group6_info | The DPL output used to populate JSON group 6. | String | optional |
| JSON group 7 | group7 | The name of the seventh JSON group. | String | optional |
| JSON group 7 data | group7_info | The DPL output used to populate JSON group 7. | String | optional |
| JSON group 8 | group8 | The name of the eighth JSON group. | String | optional |
| JSON group 8 data | group8_info | The DPL output used to populate JSON group 8. | String | optional |
| JSON group 9 | group9 | The name of the ninth JSON group. | String | optional |
| JSON group 9 data | group9_info | The DPL output used to populate JSON group 9. | String | optional |
| JSON group 10 | group10 | The name of the tenth JSON group. | String | optional |
| JSON group 10 data | group10_info | The DPL output used to populate JSON group 10. | String | optional |