Difference between revisions of "Help:Lua"

From Team Fortress Wiki
Jump to: navigation, search
m
Line 1: Line 1:
 +
== Boilerplate ==
 +
=== Passing arguments to Lua functions ===
 +
Inserting this code at the top of every module, and calling it with <nowiki>local args = get_args(frame)</nowiki> at the beginning of the function, will allow you to call the function with arguments in different ways:
 +
:The first way is the standard <nowiki>{{#invoke:Module|function_name}}</nowiki> called from a template, which passes the template arguments automatically.
 +
:The second way is to allow you to pass arguments when calling the module from another module. For example, <nowiki>p.function({arg1 = 'foo', arg2 = 'bar'});</nowiki>.
 +
:The third way is to allow you to pass arguments from the template explicitly. For example, <nowiki>{{#invoke: Module|function| arg1 = {{{param2}}} | arg2 = {{{param1}}} }}</nowiki>.
 +
 +
<syntaxhighlight lang="lua">
 +
local function get_args(frame)
 +
    local origArgs
 +
    if frame == mw.getCurrentFrame() then
 +
        -- We're being called via #invoke. If the invoking template passed any args, use
 +
        -- them. Otherwise, use the args that were passed into the template.
 +
        origArgs = frame:getParent().args
 +
        for k, v in pairs(frame.args) do
 +
            origArgs = frame.args
 +
            break
 +
        end
 +
    else
 +
        -- We're being called from another module or from the debug console, so assume
 +
        -- the args are passed in directly.
 +
        origArgs = frame
 +
    end
 +
 +
    -- ParserFunctions considers the empty string to be false, so to preserve the previous
 +
    -- behavior of the template, change any empty arguments to nil, so Lua will consider
 +
    -- them false too.
 +
    local args = {}
 +
    for k, v in pairs(origArgs) do
 +
        if v ~= '' then
 +
            args[k] = v
 +
        end
 +
    end
 +
    return args
 +
end
 +
</syntaxhighlight>
 +
 
== Key issues ==
 
== Key issues ==
 
* It is important to maintain backwards compability as templates are converted to Lua modules, to save us having to manually fix 10's or 100's of thousands of template calls. To that end it would make the most sense for existing templates to just call <nowiki>{{#invoke:Module|function}}</nowiki> which calls Module.function_name(param1, param2, ...).
 
* It is important to maintain backwards compability as templates are converted to Lua modules, to save us having to manually fix 10's or 100's of thousands of template calls. To that end it would make the most sense for existing templates to just call <nowiki>{{#invoke:Module|function}}</nowiki> which calls Module.function_name(param1, param2, ...).

Revision as of 20:40, 20 November 2013

Boilerplate

Passing arguments to Lua functions

Inserting this code at the top of every module, and calling it with local args = get_args(frame) at the beginning of the function, will allow you to call the function with arguments in different ways:

The first way is the standard {{#invoke:Module|function_name}} called from a template, which passes the template arguments automatically.
The second way is to allow you to pass arguments when calling the module from another module. For example, p.function({arg1 = 'foo', arg2 = 'bar'});.
The third way is to allow you to pass arguments from the template explicitly. For example, {{#invoke: Module|function| arg1 = {{{param2}}} | arg2 = {{{param1}}} }}.

<syntaxhighlight lang="lua"> local function get_args(frame)

   local origArgs
   if frame == mw.getCurrentFrame() then
       -- We're being called via #invoke. If the invoking template passed any args, use
       -- them. Otherwise, use the args that were passed into the template.
       origArgs = frame:getParent().args
       for k, v in pairs(frame.args) do
           origArgs = frame.args
           break
       end
   else
       -- We're being called from another module or from the debug console, so assume
       -- the args are passed in directly.
       origArgs = frame
   end

   -- ParserFunctions considers the empty string to be false, so to preserve the previous 
   -- behavior of the template, change any empty arguments to nil, so Lua will consider
   -- them false too.
   local args = {}
   for k, v in pairs(origArgs) do
       if v ~=  then
           args[k] = v
       end
   end
   return args

end </syntaxhighlight>

Key issues

  • It is important to maintain backwards compability as templates are converted to Lua modules, to save us having to manually fix 10's or 100's of thousands of template calls. To that end it would make the most sense for existing templates to just call {{#invoke:Module|function}} which calls Module.function_name(param1, param2, ...).
  • How will users now edit certain templates with edit permissions limited to staff members? This can be split into two use cases. Those who want to edit a string or translated string and those who want to edit the module code itself. Perhaps they can request changes on the Module talk page for a staff member to review and push. Or, in the latter use case, it would be more efficient to introduce a 'module editor' user group that has edit permissions for Module pages, given to trusted and capable editors.
  • Some sort of list of code conventions and guidelines should be created. A simple document for now, that can be expanded upon as issues arise.

Resources