Anonymous
Not logged in
Talk
Contributions
Create account
Log in
Tales from Chyria
Search
Editing
Module:Arguments/doc
(section)
From Tales from Chyria
Namespaces
Module
Discussion
More
More
Page actions
Read
Edit source
History
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Basic use == First, you need to load the module. It contains one function, named <code>getArgs</code>. <syntaxhighlight lang="lua"> local getArgs = require('Module:Arguments').getArgs </syntaxhighlight> In the most basic scenario, you can use getArgs inside your main function. The variable <code>args</code> is a table containing the arguments from #invoke. (See below for details.) <syntaxhighlight lang="lua"> local getArgs = require('Module:Arguments').getArgs local p = {} function p.main(frame) local args = getArgs(frame) -- Main module code goes here. end return p </syntaxhighlight> === Recommended practice === However, the recommended practice is to use a separate function as the entry point from <code>#invoke</code> just for processing the arguments. This allows other Lua modules to call your core logic directly, improving performance by avoiding the overhead of interacting with the <code>frame</code> object. <syntaxhighlight 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) -- Main module code goes here. end return p </syntaxhighlight> The way this is called from a template is <code><nowiki>{{#invoke:Example|main}}</nowiki></code> (optionally with some parameters like <code><nowiki>{{#invoke:Example|main|arg1=value1|arg2=value2}}</nowiki></code>), and the way this is called from a module is <syntaxhighlight lang=lua inline>require('Module:Example')._main({arg1 = 'value1', arg2 = value2, 'spaced arg3' = 'value3'})</syntaxhighlight>. What this second one does is construct a table with the arguments in it, then gives that table to the p._main(args) function, which uses it natively. === Multiple functions === If you want multiple functions to use the arguments, and you also want them to be accessible from #invoke, you can use a wrapper function. <syntaxhighlight lang="lua"> local getArgs = require('Module:Arguments').getArgs local p = {} local function makeInvokeFunc(funcName) return function (frame) local args = getArgs(frame) return p[funcName](args) end end p.func1 = makeInvokeFunc('_func1') function p._func1(args) -- Code for the first function goes here. end p.func2 = makeInvokeFunc('_func2') function p._func2(args) -- Code for the second function goes here. end return p </syntaxhighlight>
Summary:
Please note that all contributions to Tales from Chyria may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Chyria Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Wiki tools
Wiki tools
Page tools
Page tools
User page tools
More
What links here
Related changes
Page information
Page logs