Jump to content

Module:Sandbox/Grufo

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Uzume (talk | contribs) at 20:33, 10 July 2025 (+). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- Example module to demonstrate how it can detect how it is loaded and why such can be useful
local modname = ... or mw.getCurrentFrame():getTitle()
local api
if not ... then
	--[=[ '#invoke'
		This is where "wrapper" functionality should be so it is available to wikitext templates, etc.
	]=]
	api = require(modname) -- '#invoke' does not add the package to 'package.loaded'
	local mt = {}
	function mt.__index(_tab, key)
		return function(frame) --[=[
			This is the function from '{{#invoke:key|...}}', where 'key' is any valid string argument
			'#invoke' parses this first "function" argument differently (e.g., "=" is valid, etc.)
			and sending the rest to the frame parser
			Notice: '{{#invoke:main|...}}' is valid despite
			"main" being an API function accessible from require()
			]=]
			return api.main(frame.args, key)
		end
	end
	return setmetatable({}, mt)
else
	--[=[ 'require()' (and 'mw.loadData()'?)
		This is where "main" functionality should be so it is available to modules, including itself
	]=]
	api = {}
	function api.main(args, default)
		return modname .. ' yields ' .. (args[2] or args[1] or default)
	end
	return api
end