Module:Infobox/doc: Difference between revisions
(documenting meta = to addRow) |
(Document class config) |
||
(9 intermediate revisions by the same user not shown) | |||
Line 3:
==Creating a template step-by-step==
===Import Module:Infobox and Module:Param Parse===
<syntaxhighlight lang="lua">
local
local parse = require('Module:Param Parse')
</syntaxhighlight>
===Unpack the frame arguments from the Template===
<syntaxhighlight lang="lua">
local p = {}
function p.main(frame)
local args = frame:getParent().args
Line 15 ⟶ 18:
</syntaxhighlight>
===
<syntaxhighlight lang="lua">
local
infobox_name = 'Scenery',
class = {Infobox.smw_param('episode')}, -- Add css class with episode name to colorize Infobox
}
</syntaxhighlight>
===Map your arguments to parsing functions===
{{Main|Module:
Use the params in [[Module:Param Parse]] to validate and format the data - feel free to create your own new params in [[Module:Param Parse]].
<syntaxhighlight lang="lua">
local params = {
parse.name,
parse.image,
{name = 'examine', func = parse.has_content, smw_property = 'Examine'}, -- Custom param
parse.episode,
...
}
</syntaxhighlight>
===
{{Main|Module:Infobox#
<syntaxhighlight lang="lua">
local infobox = Infobox.new(config, params, args)
</syntaxhighlight>
===
{{Main|Module:Infobox#create}}
<syntaxhighlight lang="lua">
infobox
:add_row{
{tag='th', content=Infobox.param('name'), class='infobox-header', colspan='20'},
}
:add_row{
{tag='td', content=Infobox.param('image'), class='infobox-image', colspan='20'},
}
:pad("20")
:add_row{
{tag='td', content='Info', class='infobox-subheader', colspan='20'},
}
:pad("20")
:add_row{
{tag='th', content='Examine', colspan="6"},
{tag='td', content=Infobox.param('examine'), colspan="14"},
}
:add_row{
{tag='th', content='[[Episode]]', colspan="6"},
{tag='td', content=Infobox.param('episode'), colspan="14"},
}
...
</syntaxhighlight>
===You're done!===
<syntaxhighlight lang="lua">
return infobox
end
return p
</syntaxhighlight>
==Functions==
===
You don't need to do anything about these special parameters, but they may be used as parameters within the Template:
{| class="wikitable"
!
! explanation
|-
| version1, version2, version3
| Button label and SMW name for each switch version of the infobox
|-
| default_version
| The default version to display when the page is loaded
|-
| version
| If there is only a single version, you can use version to set the SMW name (default SMW name is "DEFAULT")
|}
=== Referring to params ===
Each parameter can have a different value for each version. In addition, there are 3 different representations of each value. Therefore, a parameter must be accessed via one of the 3 helper functions:
{| class="wikitable"
! helper function
! example
! explanation
|-
| Infobox.raw_param(name)
| "1000"
| Raw value as passed by the Template
|-
| Infobox.param(name)
| "1,000"
| Value formatted for display in the Infobox
|-
| Infobox.smw_param(name)
| 1000
| Value formatted to be saved as an SMW property
|}
=== Infobox.new(config, params, args) ===
Creates a new infobox. Automatically parses the arguments, creates SMW subobjects and adds categories.
<syntaxhighlight lang="lua">
local infobox = Infobox.new(config, params, args)
</syntaxhighlight>
==== config ====
There are only 3 parameters for config
<syntaxhighlight lang="lua">
local config = {
infobox_name = 'Scenery', -- mandatory unique identifier for css
class = {'CustomClass', Infobox.smw_param('episode')} -- optional, defaults to {}. Adds css classes to infobox table: {'infobox-CustomClass', 'infobox-[default version parameter's value]'}
max_buttons = 6, -- optional, defaults to 6, max number of switch buttons before using a dropdown list instead
}
</syntaxhighlight>
====
A list of parameters to be processed by the Infobox module
<syntaxhighlight lang="lua">
local params = {
{ name = <param>, func = <func>, ... },
...
}
</syntaxhighlight>
{| class="wikitable"
! key
! value
|-
| name
| parameter name as used in the Template
|-
| func
| A function in [[Module:Param Parse]] to validate and process the Template argument. You can also use a local function, but this is not recommended.
If func is a function, will call func(Infobox.raw_param(name)):
<syntaxhighlight lang="lua">
{name = <param>, func = <func>, ... },
</syntaxhighlight>
If func is a table, it takes the following parameters:
<syntaxhighlight lang="lua">
{name = <param>, func = { name = <func>, params = <func_params>}, ... },
</syntaxhighlight>
{| class="wikitable"
| name
| function in [[Module:Param Parse]]
|-
| params
| a list of parameters to pass the the function, in the form of constants, or Infobox.raw_param(name), Infobox.param(name), Infobox.smw_param(name)
|}
|-
| empty (optional)
| text to display in the infobox if func returns nil; defaults to "? (edit)"
|-
| category_never (optional)
| category to add if func returns nil for all versions
|-
| category_partial (optional)
| category to add if func returns nil for some versions, but a value for other versions
|-
| category_incomplete (optional)
| category to add if func returns nil for at least 1 version (i.e. category_never and category_partial combined)
|-
| category_complete (optional)
| category to add if func returns a value for all versions
|-
| smw_property (optional)
| if this string is defined, the parameter will be saved into SMW
|-
| smw_func (optional)
| function to validate and process the Template argument to save into SMW. func is used by default if smw_func is not defined
|}
==== args ====
Arguments passed via the Template
<syntaxhighlight lang="lua">
local args = frame:getParent().args
</syntaxhighlight>
=== infobox:is_param_defined(param) ===
Used to conditionally display a line in the infobox
<syntaxhighlight lang="lua">
infobox:add_row{
{tag='th', content=Infobox.param('name'), class='infobox.subheader', colspan='2'},
}
if infobox:is_param_defined(Infobox.param('owner')) > 0 then
infobox:add_row{
{tag='td', content='[[Owner]]'},
{tag='td', content=Infobox.param('owner')},
}
end
</syntaxhighlight>
{| class="wikitable"
| param
| a parameter referenced via Infobox.raw_param(name), Infobox.param(name) or Infobox.smw_param(name)
|-
| Returns
| 0 if param is never defined
1 if param is defined for a fraction of the versions
2 if param is defined for all versions
|}
=== Infobox:add_row(...) ===
Adds a row to the infobox table. Parameter should be a table of cells:
<syntaxhighlight lang="lua">
infobox:add_row{
{
{tag='td', content=Infobox.param('param'), ...},
...
addClass = 'row-class'
}
</syntaxhighlight>
Each cell should have a set of key-values, of which only the tag and content are mandatory:
{| class="wikitable"
! key
!
|-
| tag
| 'td' or 'th'
|-
| content
| a string or Infobox.param(name), Infobox.raw_param(name), Infobox.smw_param(name)
|-
| attr (optional)
| a table of attributes to add to the cell
mw.html:attr({ arg1 = '1', ... })
|-
| css (optional)
| a table of css to add to the cell
mw.html:css({ arg1 = '1', ... })
|-
| class (optional)
| a string with a class to add, or a table of classes to add. '''infobox-header''', '''infobox-subheader''' and '''infobox-image''' are commonly used.
mw.html:addClass(...)
|-
| rowspan (optional)
| Set the cell rowspan
mw.html:attr('rowspan',arg)
|-
| rowspan (optional)
| Set the cell colspan
|-
| title (optional)
| Set the cell title
|}
=== Infobox:pad(colspan, class) ===
Adds a blank row of padding spanning the given number of columns. Will always add the class '''infobox-padding''', but can optionally add another class:
<syntaxhighlight lang="lua">
infobox:pad("10", class=<class>)
</syntaxhighlight>
=== Infobox:addClass(class) ===
Adds a class to the entire table
<syntaxhighlight lang="lua">
infobox:addClass(class)
</syntaxhighlight>
=== Infobox:dump() ===
Logs all the values into the Debug console for debugging purposes. You can also dump all the values in an Infobox template by setting a template parameter "__dump = Yes".
|
Latest revision as of 15:10, 24 April 2024
Creating a template step-by-step
Import Module:Infobox and Module:Param Parse
local Infobox = require('Module:Infobox')
local parse = require('Module:Param Parse')
Unpack the frame arguments from the Template
local p = {}
function p.main(frame)
local args = frame:getParent().args
...
Setup the Module config settings
local config = {
infobox_name = 'Scenery',
class = {Infobox.smw_param('episode')}, -- Add css class with episode name to colorize Infobox
}
Map your arguments to parsing functions
Use the params in Module:Param Parse to validate and format the data - feel free to create your own new params in Module:Param Parse.
local params = {
parse.name,
parse.image,
{name = 'examine', func = parse.has_content, smw_property = 'Examine'}, -- Custom param
parse.episode,
...
}
Define an Infobox object
local infobox = Infobox.new(config, params, args)
Create your table
infobox
:add_row{
{tag='th', content=Infobox.param('name'), class='infobox-header', colspan='20'},
}
:add_row{
{tag='td', content=Infobox.param('image'), class='infobox-image', colspan='20'},
}
:pad("20")
:add_row{
{tag='td', content='Info', class='infobox-subheader', colspan='20'},
}
:pad("20")
:add_row{
{tag='th', content='Examine', colspan="6"},
{tag='td', content=Infobox.param('examine'), colspan="14"},
}
:add_row{
{tag='th', content='[[Episode]]', colspan="6"},
{tag='td', content=Infobox.param('episode'), colspan="14"},
}
...
You're done!
return infobox
end
return p
Functions
Special params
You don't need to do anything about these special parameters, but they may be used as parameters within the Template:
param | explanation |
---|---|
version1, version2, version3 | Button label and SMW name for each switch version of the infobox |
default_version | The default version to display when the page is loaded |
version | If there is only a single version, you can use version to set the SMW name (default SMW name is "DEFAULT") |
Referring to params
Each parameter can have a different value for each version. In addition, there are 3 different representations of each value. Therefore, a parameter must be accessed via one of the 3 helper functions:
helper function | example | explanation |
---|---|---|
Infobox.raw_param(name) | "1000" | Raw value as passed by the Template |
Infobox.param(name) | "1,000" | Value formatted for display in the Infobox |
Infobox.smw_param(name) | 1000 | Value formatted to be saved as an SMW property |
Infobox.new(config, params, args)
Creates a new infobox. Automatically parses the arguments, creates SMW subobjects and adds categories.
local infobox = Infobox.new(config, params, args)
config
There are only 3 parameters for config
local config = {
infobox_name = 'Scenery', -- mandatory unique identifier for css
class = {'CustomClass', Infobox.smw_param('episode')} -- optional, defaults to {}. Adds css classes to infobox table: {'infobox-CustomClass', 'infobox-[default version parameter's value]'}
max_buttons = 6, -- optional, defaults to 6, max number of switch buttons before using a dropdown list instead
}
params
A list of parameters to be processed by the Infobox module
local params = {
{ name = <param>, func = <func>, ... },
...
}
key | value | ||||
---|---|---|---|---|---|
name | parameter name as used in the Template | ||||
func | A function in Module:Param Parse to validate and process the Template argument. You can also use a local function, but this is not recommended.
If func is a function, will call func(Infobox.raw_param(name)): {name = <param>, func = <func>, ... },
If func is a table, it takes the following parameters: {name = <param>, func = { name = <func>, params = <func_params>}, ... },
| ||||
empty (optional) | text to display in the infobox if func returns nil; defaults to "? (edit)" | ||||
category_never (optional) | category to add if func returns nil for all versions | ||||
category_partial (optional) | category to add if func returns nil for some versions, but a value for other versions | ||||
category_incomplete (optional) | category to add if func returns nil for at least 1 version (i.e. category_never and category_partial combined) | ||||
category_complete (optional) | category to add if func returns a value for all versions | ||||
smw_property (optional) | if this string is defined, the parameter will be saved into SMW | ||||
smw_func (optional) | function to validate and process the Template argument to save into SMW. func is used by default if smw_func is not defined |
args
Arguments passed via the Template
local args = frame:getParent().args
infobox:is_param_defined(param)
Used to conditionally display a line in the infobox
infobox:add_row{
{tag='th', content=Infobox.param('name'), class='infobox.subheader', colspan='2'},
}
if infobox:is_param_defined(Infobox.param('owner')) > 0 then
infobox:add_row{
{tag='td', content='[[Owner]]'},
{tag='td', content=Infobox.param('owner')},
}
end
param | a parameter referenced via Infobox.raw_param(name), Infobox.param(name) or Infobox.smw_param(name) |
Returns | 0 if param is never defined
1 if param is defined for a fraction of the versions 2 if param is defined for all versions |
Infobox:add_row(...)
Adds a row to the infobox table. Parameter should be a table of cells:
infobox:add_row{
{tag='td', content='[[Cell1]]', ...},
{tag='td', content=Infobox.param('param'), ...},
{tag='td', content='[[Cell3]]', ...},
...
addClass = 'row-class'
}
Each cell should have a set of key-values, of which only the tag and content are mandatory:
key | value |
---|---|
tag | 'td' or 'th' |
content | a string or Infobox.param(name), Infobox.raw_param(name), Infobox.smw_param(name) |
attr (optional) | a table of attributes to add to the cell
mw.html:attr({ arg1 = '1', ... }) |
css (optional) | a table of css to add to the cell
mw.html:css({ arg1 = '1', ... }) |
class (optional) | a string with a class to add, or a table of classes to add. infobox-header, infobox-subheader and infobox-image are commonly used.
mw.html:addClass(...) |
rowspan (optional) | Set the cell rowspan
mw.html:attr('rowspan',arg) |
rowspan (optional) | Set the cell colspan
mw.html:attr('colspan',arg) |
title (optional) | Set the cell title
mw.html:attr('title',arg) |
Infobox:pad(colspan, class)
Adds a blank row of padding spanning the given number of columns. Will always add the class infobox-padding, but can optionally add another class:
infobox:pad("10", class=<class>)
Infobox:addClass(class)
Adds a class to the entire table
infobox:addClass(class)
Infobox:dump()
Logs all the values into the Debug console for debugging purposes. You can also dump all the values in an Infobox template by setting a template parameter "__dump = Yes".