Module:template: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
local export = {}
local export = {}


local function make_template(frame, nested)
function export.make_template(args, nested)
local args = (frame:getParent() and frame:getParent().args) or frame.args
if not args[1] then error("at least one argument must be given to {{temp}}") end
local out = {(nested and "") or "<code>", "&#123;&#123;", args[1]}
local out = {(nested and "") or "<code>", "&#123;&#123;[[Template:" .. args[1] .. "|" .. args[1] .. "]]"}
local i = 2
local i = 2
while args[i] do
while args[i] do
Line 9: Line 9:
i = i + 1
i = i + 1
end
end
for key, value in ipairs(args) do
for key, value in require("Module:table").sortedPairs(args) do
if type(key) == "string" then
if type(key) == "string" then
table.insert(out, "&#124;" .. key .. "=" .. value)
table.insert(out, "&#124;" .. key .. "=" .. value)
Line 19: Line 19:


function export.show(frame)
function export.show(frame)
return make_template(frame, false)
return export.make_template(frame:getParent().args, false)
end
end


function export.showNested(frame)
function export.showNested(frame)
return make_template(frame, true)
return export.make_template(frame:getParent().args, true)
end
end


return export
return export

Latest revision as of 14:37, 7 August 2023

Documentation for this module may be created at Module:template/documentation

local export = {}

function export.make_template(args, nested)
	if not args[1] then error("at least one argument must be given to {{temp}}") end
	local out = {(nested and "") or "<code>", "&#123;&#123;[[Template:" .. args[1] .. "|" .. args[1] .. "]]"}
	local i = 2
	while args[i] do
		table.insert(out, "&#124;" .. args[i])
		i = i + 1
	end
	for key, value in require("Module:table").sortedPairs(args) do
		if type(key) == "string" then
			table.insert(out, "&#124;" .. key .. "=" .. value)
		end
	end
	table.insert(out, "&#125;&#125;" .. ((nested and "") or "</code>"))
	return table.concat(out)
end

function export.show(frame)
	return export.make_template(frame:getParent().args, false)
end

function export.showNested(frame)
	return export.make_template(frame:getParent().args, true)
end

return export