The Graveyard Wiki:Lua

Lua is a programming language that is supported within Mediawiki and Fandom. Although there is very little it can do that parser functions and other extensions cannot, it is faster than calling multiple functions at once. In some wikis the Variable extension is enabled. This does allow wiki pages to be programmatic, but is not the preferred method of writing templates for a variety of reasons: The only real benefit of variables over Lua is that the template with a variable declaration needs to only be on the same page as the template using it. This in itself is also a weakness due to the nature of editing where a template could be removed from a page and in essence break the other template in the process. This then makes it harder still to track down the issue.
 * Complex templates are harder to read.
 * Variables can be difficult to track when making template changes (the declaration and application can be in different places).

Lua scripts are stored in the  namespace and are called an a page using the parser function.

A basic Lua function
At their most basic a Lua function has three parts:
 * A variable
 * A function
 * A return

A simple Lua function that returns the words "hello world" would be written as follows:

Working example: Module:Hello World.

In the example script The first line is a variable, lines 3 - 5 are the function (Begins  and finishes with  ) the return is the final line.

Variables
Variables can be declared without  which means they can be accessed by other functions. In practice if a Lua function is isolated from other functions (doesn't call/isn't called by another function) not declaring  isn't an issue as no other function is going to access it in error, but it is strongly recommended that variables are declared in the majority of cases as a method of future proofing.

The variable declared in our example should always be declared as  as this is used to store all functions which return a result to a template or article.

Variable Placement
Variables need to be declared (e.g. ) or created (e.g.  ) before the point of use in the code. For example: Will cause an error as  does not yet exist; the code will handle this as   (  is the Lua equivalent of   that most will be more familiar with).

The following code will also fail because  has been declared, but not set and is also treated as  :

The following will work as  has been set to 5 and will return 25:

If a variable needs to be accessible to multiple functions within a script, this can be placed above the functions in the script:

Functions
Every function, like the main script has a minimum of three parts:
 * Declaration
 * Return
 * End

With declarations, these are in the format of. When pulling parameters from a page or template the function needs to declare the  object to bring them into the Lua function(e.g.  ); the name of the variable(s) otherwise can be anything and passing multiple variables can be done with comma separation (e.g.

Internal and External Functions
In our hello world example the function is declared as. The  prefix at the start of the function states that the result is stored in the variable to be returned to the template/article and as such is an external function. this must match the name of the variable; for example if the variable was  the function would be. An external function with a variable other then  can still be used to pass variables from another module if the function being called has been imported using the   function.

Internal functions are accessible only by the Lua script they are contained within and are identified by the fact they do not have a prefix at the start of the function. These can only be called by other functions within the same module page.

Best practice
When writing functions the best practice would be to have the main script within an internal function and two external functions where information is being passed into the function: one that uses the  object and one with standard variables:

Frame Object
The  mentioned in the latter section about functions is more than just a variable. For the full usage see the documentation on MediaWiki.

From a beginners standpoint, the  object passes the parameters from the   function to be used within the code itself. For intermediate and advanced users it can also be used to call wiki templates and parser functions for use.

Once the frame object has been called into a function, its functions can be accessed using the  parameter. In our last example, in function  we are setting the variable   as. The number in  is equivalent to the parameter   in a wikitext template.

In the example,   would be "hi". If named parameters are desired in an invoke call, these would be entered in the same way as when setting parameters to a wiki text template. For example the function call  has a parameter of   with a value of "hi". To calling this parameter in Lua would be written as :

It is worth noting that any numbers passed to a function from the  object will be in string form rather than number. the following script will always fail when a number is passed through:

To make the number usable, the value needs to be converted from a string to a number:

Working example: Module:Math.

Data Tables
In the simplest form, a data table is a collection of key/value pairs  in all previous examples is an example of an empty data table; the   would contain any values in the table set.

A basic data table would look as follows:

On each line there is a single key/value pair and a comma to denote the end of one pair and the start of the next pair.

Calling an item from a data table uses the syntax. For the data table above, if bleep was to be called the syntax would be  A basic example would be:

Data tables can also have sub tables, allowing for the storage of related data in one place. Expanding on our original dataset:

We can now retrieve two pieces of information from the same place:

Note how in the above example there are two '[]' entries, when using sub tables, the primary table item must be declared first, then the sub table item. If there were another sub table within the first sub table, all three table items would be declared

As with other variables, if a data table is required by several functions, it can be placed outside the function. For large datasets that are liable to needing updaters, it may be better to hold these on a subpage of the module to make code management easier.

Another method of accessing the sub table above would be to assign the contents of the primary table item to a new variable (which only contains the sub table) and use that variable to place the data instead:

Working Examples: Module:Users