Module:Arguments/doc: Difference between revisions

From Mariopedia, a wiki on Mario, Yoshi, Wario, Donkey Kong, Super Smash Bros., and more!
Jump to navigationJump to search
add info about frames and parent frames
(add valueFunc info)
(add info about frames and parent frames)
Line 139:
 
Note: if you are using the <code>getArgs</code> function in the main function of your module, be aware that if it is called from another Lua module then <code>value</code> might not always be a string. This is not a problem if you are using a function specially for arguments from #invoke (i.e. you have <code>p.main</code> and <code>p._main</code> functions, or something similar).
 
=== Frames and parent frames ===
 
Arguments in the <code>args</code> table can be passed from the current frame or from its parent frame at the same time. To understand what this means, it is easiest to give an example. Let's say that we have a module called <code>Module:ExampleArgs</code>. This module prints the first three positional arguments that it is passed.
 
{{cot|Module:ExampleArgs code}}
<source lang="lua">
local getArgs = require('Module:Arguments').getArgs
local p = {}
 
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
 
function p._main(args)
local first = args[1] or ''
local second = args[2] or ''
local third = args[3] or ''
return first .. ' ' .. second .. ' ' .. third
end
 
return p
</source>
{{cob}}
 
<code>Module:ExampleArgs</code> is then called by <code>Template:ExampleArgs</code>, which contains the code <code><nowiki>{{#invoke:ExampleArgs|main|firstArg}}</nowiki></code>. This produces the result "firstArg".
 
Now if we were to call <code>Template:ExampleArgs</code>, the following would happen:
 
# <code><nowiki>{{ExampleArgs}}</nowiki></code> &rarr; firstArg
# <code><nowiki>{{ExampleArgs|myFirstArg}}</nowiki></code> &rarr; firstArg
# <code><nowiki>{{ExampleArgs|myFirstArg|mySecondArg|myThirdArg}}</nowiki></code> &rarr; firstArg mySecondArg myThirdArg
 
If you want the value from the parent frame to have precedence over the value from the current frame (i.e. if you would like "myFirstArg" instead of "firstArg" in the second example above), you can do so with the <code>parentFirst</code> option.
 
<source lang="lua">
local args = getArgs(frame, {
parentFirst = true
})
</source>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu