Module:Sandbox/Grufo
Appearance
This is an example module to demonstrate how a module can detect how it is loaded and why such can be useful.
- Module:Sandbox/Grufo yields
- Module:Sandbox/Grufo yields
- Module:Sandbox/Grufo yields
- Module:Sandbox/Grufo yields b
- Module:Sandbox/Grufo yields =
- Module:Sandbox/Grufo yields =
- Module:Sandbox/Grufo yields b
-- 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