Module:InfoboxBuilder
| This module is rated as alpha. It is ready for limited use and third-party feedback. It may be used on a small number of pages, but should be monitored closely. Suggestions for new features or adjustments to input and output are welcome. |
| This module is currently under extended confirmed protection. Extended confirmed protection prevents edits from all unregistered editors and registered users with fewer than 30 days tenure and 500 edits. The policy on community use specifies that extended confirmed protection can be applied to combat disruption, if semi-protection has proven to be ineffective. Extended confirmed protection may also be applied to enforce arbitration sanctions. Please discuss any changes on the talk page; you may submit an edit request to ask for uncontroversial changes supported by consensus. |
This module provides a builder-style interface to Module:Infobox, which allows you to dynamically construct infoboxes without having to wrangle row numbers. Currently this can only be used by other modules.
Overview
This module provides a single class named InfoboxBuilder, which you can create an instance of by calling .new().
local InfoboxBuilder = require('Module:InfoboxBuilder').InfoboxBuilder
-- later on in the module:
function p.someFunction( frame )
local builder = InfoboxBuilder.new()
end
| Above | |
|---|---|
| Below |
InfoboxBuilder has two basic features: firstly, it functions as an accumulator for arguments to Module:Infobox, e.g.
InfoboxBuilder.new()
:bodystyle("background-color: #FFD3D3")
:title("Title")
:above("Above")
:below("Below")
:build()
is the equivalent of
frame:expandTemplate{ title = 'Infobox', args = {
["bodystyle"] = "background-color: #FFD3D3",
["title"] = "Title",
["above"] = "Above",
["below"] = "Below"
}}
and secondly, it takes care of numbering your subheaders/images/rows for you, e.g.
| Subheader 1 | |
| Subheader 2 | |
![]() | |
![]() This image has a caption | |
| Header 1 | |
|---|---|
| Label | Data |
| Only Data | |
| Header 2 | |
| Fancy Row? | Yes |
InfoboxBuilder.new()
:title("Title")
:subheader("Subheader 1")
:subheader{
subheader = "Subheader 2",
subheaderstyle = "color: red"
}
:image("[[File:Example.jpg|upright=0.4]]")
:image{
image = "[[File:Example-serious.jpg|upright=0.4]]",
caption = "This image has a caption"
}
:header("Header 1")
:row("Data", "Label")
:row("Only Data")
:header{
header = "Header 2",
rowstyle = "background-color: #FF746C"
}
:row{
data = "Yes",
label = "Fancy Row?",
rowstyle = "background-color: #FFFFC5"
}
:build()
is the equivalent of
frame:expandTemplate{ title = 'Infobox', args = {
["title"] = "Title",
["subheader1"] = "Subheader 1",
["subheader2"] = "Subheader 2",
["subheaderstyle2"] = "color: red"
["image1"] = "[[File:Example.jpg|upright=0.4]]",
["image2"] = "[[File:Example-serious.jpg|upright=0.4]]",
["caption2"] = "This image has a caption",
["header1"] = "Header 1",
["label2"] = "Label",
["data2"] = "Data",
["data3"] = "Only Data",
["header4"] = "Header 2",
["rowstyle4"] = "background-color: #FF746C"
["label5"] = "Fancy Row?",
["data5"] = "Yes",
["rowstyle5"] = "background-color: #FFFFC5"
}}
Usage
The InfoboxBuilder class acts as an accumulator for parameters to Module:Infobox which will be passed along once you invoke the InfoboxBuilder:build() function. All non-repeated parameters (e.g., title, below, subheaderstyle) can be set by calling InfoboxBuilder:parameter(value) where parameter is the parameter name and value is the parameter value[a] (note that italic title should be passed along using InfoboxBuilder:italicTitle(value) as a special case since it contains a space. Similarly, all repeated parameters (e.g., header(n), label(n), etc.) can be set using the subheader, image, header, and row functions, each of which accept all parameters which should be linked together at the same time (these parameters can be passed as either positional arguments or named arguments, see below. All functions support chaining.
InfoboxBuilder:subheaderInfoboxBuilder:subheader(subheader, subheaderstyle, subheaderrowclass)InfoboxBuilder:subheader{ subheader = subheader, subheaderstyle = subheaderstyle, subheaderrowclass = subheaderrowclass }
InfoboxBuilder:imageInfoboxBuilder:image(image, caption, imagerowclass)InfoboxBuilder:image{ image = image, caption = caption, imagerowclass = imagerowclass }
InfoboxBuilder:headerInfoboxBuilder:header(header, rowclass, rowstyle)InfoboxBuilder:header{ header = header, rowclass = rowclass, rowstyle = rowstyle }
InfoboxBuilder:rowInfoboxBuilder:row(data, label, class, rowclass, rowstyle, rowcellstyle)InfoboxBuilder:row{ data = data, label = label, class = class, rowclass = rowclass, rowstyle = rowstyle, rowcellstyle = rowcellstyle }
- ^ This module supports all parameters supported by Module:Infobox.
local infobox = require("Module:Infobox")
local InfoboxBuilder = {}
-- Create a new InfoboxBuilder
function InfoboxBuilder.new ()
-- store parameters and counters
local obj = {
params = {},
iter_subheader = 1,
iter_image = 1,
iter_row = 1
}
-- Use InfoboxBuilder:__set as a "fallback" function,
-- Any call InfoboxBuilder:`x`(`y`) where InfoboxBuilder:`x` does not exist is equivalent to calling InfoboxBuilder:__set(`x`, `y`)
return setmetatable(obj, {
__index = function(tbl, key)
-- First, return any defined InfoboxBuilder:`x`
local val = InfoboxBuilder[key]
if val ~= nil then
return val
end
-- Otherwise, return a wrapper for InfoboxBuilder:__set
return function ( self, val )
return self:__set(key, val)
end
end
})
end
-- Set a parameter by name (for internal use)
function InfoboxBuilder:__set ( key, val )
self.params[key] = val
return self
end
-- Special handling for 'italic title' since it has a space
-- 'italicTitle' -> 'italic title'
function InfoboxBuilder:italicTitle ( italicTitle )
self.params["italic title"] = italicTitle
return self
end
-- Add a subheader to the infobox
-- Supports being called as :subheader("subheader", "subheaderstyle", "subheaderrowclass") and as
-- :subheader{subheader="subheader", subheaderstyle="subheaderstyle", subheaderrowclass="subheaderrowclass"}
function InfoboxBuilder:subheader ( subheader, subheaderstyle, subheaderrowclass )
-- support positional and named arguments
if type(subheader) == "table" then
return self:subheader(
subheader["subheader"],
subheader["subheaderstyle"],
subheader["subheaderrowclass"]
)
end
self.params["subheader" .. self.iter_subheader] = subheader
self.params["subheaderstyle" .. self.iter_subheader] = subheaderstyle
self.params["subheaderrowclass" .. self.iter_subheader] = subheaderrowclass
self.iter_subheader = self.iter_subheader + 1
return self
end
-- Add an image to the infobox
-- Supports both positional and named arguments just like :subheader
function InfoboxBuilder:image ( image, caption, imagerowclass )
-- support positional and named arguments
if type(image) == "table" then
return self:image(
image["image"],
image["caption"],
image["imagerowclass"]
)
end
self.params["image" .. self.iter_image] = image
self.params["caption" .. self.iter_image] = caption
self.params["imagerowclass" .. self.iter_image] = imagerowclass
self.iter_image = self.iter_image + 1
return self
end
-- Add a header row to the infobox
-- Supports both positional and named arguments just like :subheader
function InfoboxBuilder:header ( header, rowclass, rowstyle )
-- support positional and named arguments
if type(header) == "table" then
return self:header(
header["header"],
header["rowclass"],
header["rowstyle"]
)
end
self.params["header" .. self.iter_row] = header
self.params["rowclass" .. self.iter_row] = rowclass
self.params["rowstyle" .. self.iter_row] = rowstyle
self.iter_row = self.iter_row + 1
return self
end
-- Add a row to the infobox
-- Supports both positional and named arguments just like :subheader
function InfoboxBuilder:row ( data, label, class, rowclass, rowstyle, rowcellstyle )
-- support positional and named arguments
if type(data) == "table" then
return self:row(
data["data"],
data["label"],
data["class"],
data["rowclass"],
data["rowstyle"],
data["rowcellstyle"]
)
end
self.params["data" .. self.iter_row] = data
self.params["label" .. self.iter_row] = label
self.params["class" .. self.iter_row] = class
self.params["rowclass" .. self.iter_row] = rowclass
self.params["rowstyle" .. self.iter_row] = rowstyle
self.params["rowcellstyle" .. self.iter_row] = rowcellstyle
self.iter_row = self.iter_row + 1
return self
end
-- Build the infobox
-- Equivalent to calling require("Module:Infobox").infobox{ ... } where `...` is all arguments previously provided
function InfoboxBuilder:build ()
return infobox.infobox(self.params)
end
return {
InfoboxBuilder = InfoboxBuilder
}
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.

