Module:ArgsUtil: Difference between revisions

From Sword of Moonlight Wiki
Jump to navigation Jump to search
dlw>RheingoldRiver
Created page with "local p = {} function p.merge() local f = mw.getCurrentFrame() local origArgs = f.args local parentArgs = f:getParent().args local args = {} for k, v in pairs(origArgs) do v = mw.text.trim(tostring(v)) if v ~= '' then args[k] = v end end for k, v in pairs(parentArgs) do v = mw.text.trim(v) if v ~= '' then args[k] = v end end return args end return p"
 
m 1 revision imported
 
(No difference)

Latest revision as of 18:53, 12 September 2024

This is a basic module for processing args. Usage:

local util_args = require('Module:ArgsUtil')

local p = {}

function p.main(frame)
  local args = util_args.merge() -- it is not necessary to pass a `frame` object; `merge` will generate its own
  mw.logObject(args)
  -- `args` now contains args passed via the template merged with defaults provided directly via the invoke.
  -- anything the user sent via the template will be given priority.
end

return p

Extending this module

By design, {{ArgsUtil}}m is shipped with only a single function and no customization available; this simplifies documentation and covers nearly all use cases. An extended version of this module is available at Module:ArgsUtil on the support wiki if you want additional functionality; and you can of course feel free to modify this module on your own wiki as needed.


local p = {}

function p.merge()
	local f = mw.getCurrentFrame()
	local origArgs = f.args
	local parentArgs = f:getParent().args

	local args = {}
	
	for k, v in pairs(origArgs) do
		v = mw.text.trim(tostring(v))
		if v ~= '' then
			args[k] = v
		end
	end
	
	for k, v in pairs(parentArgs) do
		v = mw.text.trim(v)
		if v ~= '' then
			args[k] = v
		end
	end
	
	return args
end

return p