Модуль:Example

Для документации этого модуля может быть создана страница Модуль:Example/doc

local p = {}

-- используется для того, чтобы можно было удалять элементы из таблицы
local function copy(other)
	local res = {}
	for k,v in pairs(other) do 
		res[k] = v
	end
	return res
end

-- вызов шаблона, при ошибке возвращает пустую строку
local function expand(frame, tname, targs)
	local success, result = pcall(
		frame.expandTemplate,
		frame,
		{title = tname, args = targs}
	)
	if success then
		return result
	else
		return ''
	end
	--return frame:expandTemplate({title = tname, args = args})
end	


function p.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end	
	local args = copy(getArgs(frame)) --copy(frame.args)
	local tag =  args._tag or 'code'
	local sep =  args._sep or '→' -- по умолчанию "→"
	local nwt = mw.html.create(tag):tag(tag) --"no-wiki tag", внутри него шаблон не вызывается
	local content = '{{' --для накопления содержимого тэга 
	local tname = args._template or args[1] 
	
	if args._template == nil then --имя вызываемого шаблона в неименованном первом параметре, больше его обрабатывать не надо
		table.remove(args,1)		
	end
	content = content .. tname	
	local targs = {}	
	for k, v in pairs(args) do
		if type(k) == 'number' then --неименованные параметры
			targs[k] = v
			content = content .. '|' .. v
		elseif not k:find('^_') then --именованные параметры, исключая модификаторы внешнего вида
			targs[k] = v
			content = content .. '|' .. k .. '=' .. v
		end
	end
	content = content .. '}}'
	nwt:wikitext(content):done()

	return tostring(nwt) .. ' ' .. sep .. ' ' .. tostring(expand(frame, tname, targs))
end

return p