Creating Lua modules

Also see: Creating templates

This is a quick guide to creating a lua module.

Prerequisites
First of all, make sure you have the Scribunto extension enabled on your wiki (it is enabled on the English Wikipedia and all Miraheze wikis by default), as it implements module pages.

Creating a super basic module
Next, you need to name your module. For this example, our module would be named "Calculator". Go to the page "Module:Calculator" and create it. Now, in there, input this in:

local p = {}

function p.plus( frame ) return "Test" end

return p

"p" is the stuff that is the output from your module. So,  clears the output that is left by the last usage of the module so the new usage of the module outputs the new output, not the old output combined with the new output.

Functions are noted with, with "name" being replaced with the name of the function. adds something to "p" which as mentioned above, is the output of the module. In this example,  is after , so this function will output "Test".

Now, we saved the output as a "table" (which is p), but we haven't given p to Scribunto to turn it into wikitext and output it yet. does exactly that.

Alright, now you save the page. Now go to a non-module page (page with its name not starting with "Module:"), and put in, with "module" replaced with the name of your Lua module (in this case Calculator) and "function" being replaced with the name of the function in your module you want Scribunto to run (in this case plus), and save it or click preview. You should see it output whatever you put into the return (in this case Test because we put in ).

Frame
Have you noticed the  on the  ? Don't delete it, as this is very useful for advanced modules.

We can create parameters that the module will receive upon usage of it. For example, put in this code:

local p = {}

function p.plus( frame ) return tonumber(frame.args[1]) + tonumber(frame.args[2]) end

return p

That is simply something that can add (plus) numbers. For this example, putting in  will result in 12 because...well...7 plus 5 equals 12. This is because  is 7 and   is 5 (as we have put it in the invoke code).

Multiple functions in one module
A module can include a lot of functions, not just one. This will work: local p = {}

function p.plus( frame ) return tonumber(frame.args[1]) + tonumber(frame.args[2]) end

function p.minus( frame ) return tonumber(frame.args[1]) - tonumber(frame.args[2]) end

function p.multiply( frame ) return tonumber(frame.args[1]) * tonumber(frame.args[2]) end

function p.divide( frame ) return tonumber(frame.args[1]) / tonumber(frame.args[2]) end

return p

This will result in

--> 12

--> 2

--> 35

--> 1.4

etc.

Usage in templates
Modules can be used to create advanced templates because the invoke code parameters support...well..."wikitext parameters", so for example, this will work:

Template:Calculator

which is

--> 12

--> 2

--> 35

--> 1.4

Many of the more useful templates in the English Wikipedia (infobox, navbox, mbox, and userbox for example) are based on lua modules.

Quick summary
Lua modules are added by the Scribunto extension and are in the  namespace, they are coded in Lua, "p" is the output of a module, a function is well...a function - something that a module can do, "frame" can be used to get "parameters" from the invoke code, a module can include multiple functions, and modules are useful in templates.