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 on ref tags and known limitations, add type checking examples for the valueFunc option
(add info on writing to the args table)
(add info on ref tags and known limitations, add type checking examples for the valueFunc option)
Line 3:
* Arguments can be passed by both the current frame and by the parent frame at the same time. (More details below.)
* Arguments can be passed in directly from another Lua module or from the debug console.
* Arguments are fetched as needed, which can help avoid (some) problems with {{tag|ref}} tags.
* Most features can be customized.
 
Line 138 ⟶ 139:
</source>
 
Note: the above functions will fail if youpassed areinput that is not of type <code>string</code>. This might be the case if you usinguse the <code>getArgs</code> function in the main function of your module, be awareand that if itfunction is called fromby another Lua module. thenIn <code>value</code>this mightcase, notyou alwayswill beneed ato stringcheck the type of your input. 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).
 
{{cot|Examples 1 and 2 with type checking}}
Example 1:
<source lang="lua">
local args = getArgs(frame, {
valueFunc = function(key, value)
if key == 1 then
return value
elseif type(value) == 'string' then
value = mw.text.trim(value)
if value ~= '' then
return value
end
else
return value
end
end
})
</source>
 
Example 2:
<source lang="lua">
local args = getArgs(frame, {
valueFunc = function(key, value)
if type(value) == 'string' then
value = mw.ustring.lower(value)
if mw.ustring.find(value, '%S') then
return value
end
else
return value
end
end
})
</source>
{{cob}}
 
=== Frames and parent frames ===
Line 190 ⟶ 227:
 
It is possible to alter this behaviour with the <code>readOnly</code> and <code>noOverwrite</code> options. If <code>readOnly</code> is set then it is not possible to write any values to the args table at all. If <code>noOverwrite</code> is set, then it is possible to add new values to the table, but it is not possible to add a value if it would overwrite any arguments that are passed from #invoke.
 
=== Ref tags ===
 
This module uses [[mw:Extension:Scribunto/Lua reference manual#Metatables|metatables]] to fetch arguments from #invoke. This allows access to both the frame arguments and the parent frame arguments without using the <code>pairs()</code> function. This can help if your module might be passed {{tag|ref}} tags as input.
 
As soon as {{tag|ref}} tags are accessed from Lua, they are processed by the MediaWiki software and the reference will appear in the reference list at the bottom of the article. If your module proceeds to omit the reference tag from the output, you will end up with a phantom reference - a reference that appears in the reference list, but no number that links to it. This has been a problem with modules that use <code>pairs()</code> to detect whether to use the arguments from the frame or the parent frame, as those modules automatically process every available argument.
 
This module solves this problem by allowing access to both frame and parent frame arguments, while still only fetching those arguments when it is necessary. The problem will still occur if you use <code>pairs(args)</code> elsewhere in your module, however.
 
=== Known limitations ===
 
The use of metatables comes with its downsides, however. Most of the normal Lua table tools won't work properly on the args table, including the <code>#</code> operator, the <code>next()</code> function, and the functions in the table library. If using these is important for your module, you should use your own argument processing function instead of this module. See [[Module:Toolbar]] for a basic example of such a function.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu