Module:Infobox
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 = 'description', func = parse.has_content, smw_property = 'Description'}, -- 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='Description', colspan="6"},
{tag='td', content=Infobox.param('description'), 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".
--[=[
-- For documentation, see [[Module:Infobox/doc]]
--]=]
-- <nowiki>
local smw = require('Module:SMW Utils')
local Infobox = {}
Infobox.__index = Infobox
--[[
Infobox class
-- args : parameters from frame to pass through
-- Sets a meta table and creates a <div> tag wrapper
-- other fields are initialized in other functions
--]]
function Infobox.new(args)
local obj = setmetatable({
args = args, -- parameters (uncleaned)
infobox_name = nil, -- template name
param_names = {}, -- ordered param list
params = {}, -- param definitions
versions = nil, -- number of versions
version_names = {}, -- title of each version (for selection and SMW)
errors = {} -- list of errors
},
Infobox)
obj:parse_versions()
return obj
end
-- Defines an infobox name ({{Template:arg}})
-- Used to create a link at the bottom of pages and for css
function Infobox:define_name(arg)
self.infobox_name = arg
end
--[[
Function for defining parameters
-- name : parameter name
-- func : function to define param, defaults to looking at blanks
DO NOT DEFINE VERSION HERE
USE :maxVersion()
Can be used any number of times for efficient definition
--]]
function Infobox:define_params(...)
for _, v in ipairs(...) do
-- For every parameter, store its corresponding function to self.params
if v.name then
local param = {}
-- Copy the function
if type(v.func) == 'function' or type(v.func) == 'table' then
param.func = v.func
end
-- If smw_property is defined, then use smw_func, or default to func if it is not defined
if v.smw_property and type(v.smw_property == 'string') then
param.smw_property = v.smw_property
if type(v.smw_func) == 'function' or type(v.smw_func) == 'table' then
param.smw_func = v.smw_func
else
param.smw_func = param.func
end
end
self.params[v.name] = param
table.insert(self.param_names, v.name)
end
end
return self
end
--[[
Counts the number of versions in the infobox
Also validates the names of all the versions to be valid SMW subobjects
--]]
function Infobox:parse_versions()
-- Count the versions
local i = 1
while self.args['version'..i] do
-- Validate and register the version name
local version_name = self.args['version'..i]
if not smw.valid_subobject_name(version_name) then
table.insert(self.errors, 'Invalid Version Name (must not be "0", "" or contain a "." in the first 5 characters): version'..i..'='..version_name)
version_name = '###'..i..'_'..version_name
end
table.insert(self.version_names, version_name)
i = i + 1
end
self.versions = i - 1
end
function Infobox.param(paramname)
param = {
property = 'Param',
name = paramname,
}
return param
end
function Infobox.raw_param(paramname)
param = {
property = 'Raw Param',
name = paramname,
}
return param
end
function Infobox:dump()
mw.log(mw.dumpObject(self))
end
return Infobox