Module:TemplateStructure: Difference between revisions
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
--[[ | --[[ | ||
| Line 19: | Line 5: | ||
Renders a table using a modular, block-based approach via the HTML builder API. | Renders a table using a modular, block-based approach via the HTML builder API. | ||
Instead of returning wiki table markup (which is later reinterpreted and escaped), | |||
so that | this version builds and returns an mw.html object so that the output is treated as raw HTML. | ||
Parameters: | Parameters: | ||
| Line 30: | Line 16: | ||
Default: { cellpadding = "2" } | Default: { cellpadding = "2" } | ||
blocks: (array) An ordered list of functions (blocks) that generate table rows. | blocks: (array) An ordered list of functions (blocks) that generate table rows. | ||
Each block function should accept (args, config) and return | Each block function should accept (args, config) and return an HTML fragment. | ||
Returns: | Returns: | ||
An mw.html object representing the complete table. | |||
NOTE: | |||
This approach expects that the block functions produce HTML (or HTML fragments) rather than | |||
wiki markup. For example, instead of returning "{| ..." rows and "|}" markers, block functions | |||
should generate <tr>, <td>, or <th> elements. This change moves layout control into Lua so that | |||
MediaWiki’s parser won’t rewrap or escape the output. | |||
]] | ]] | ||
function p.render(args, config) | function p.render(args, config) | ||
| Line 41: | Line 33: | ||
local blocks = config.blocks or {} | local blocks = config.blocks or {} | ||
-- Create a table element | -- Create a table element using the HTML builder API. | ||
local tableEl = mw.html.create("table", tableAttrs) | local tableEl = mw.html.create("table", tableAttrs) | ||
tableEl:attr("class", tableClass) | tableEl:attr("class", tableClass) | ||
-- | -- Iterate over each block function, and insert its output as wikitext (which here is assumed to be safe HTML). | ||
for i, block in ipairs(blocks) do | for i, block in ipairs(blocks) do | ||
if type(block) == "function" then | if type(block) == "function" then | ||
| Line 57: | Line 49: | ||
end | end | ||
-- Return the | -- Return the mw.html object so that its output is not re-escaped. | ||
return | return tableEl | ||
end | end | ||
return p | return p | ||