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
Content deleted Content added
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: | 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 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 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. |
* Most features can be customized. |
||
Line 138: | Line 139: | ||
</source> |
</source> |
||
Note: if |
Note: the above functions will fail if passed input that is not of type <code>string</code>. This might be the case if you use the <code>getArgs</code> function in the main function of your module, and that function is called by another Lua module. In this case, you will need to check 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 === |
=== Frames and parent frames === |
||
Line 190: | Line 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. |
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. |