Lua templating/Tips and tricks

This page lists various tips and tricks for the creation, modding, and debugging of Lua-powered templates.

Lua style guide
It is usually a good idea to maintain some consistency in the design of modules, especially those used on a lot of pages. Wikipedia's Lua style guide is a good starting point for wikis creating their own guide.

Loading and parsing data structured data
The easiest way to load data is by saving it in a Lua table, storing it in a page and using  or. But sometimes it may be useful to keep the data as it is and parse it. For example when retrieving JSON data, one way to quickly retrieve everything is to store the json in a page like Dev Wiki:Storage.js and then retrieve it using  and the decode function of the Module:Json or parsing it using Lua's string library.

Note this will be an expensive call and will register as a link to that page.

Types of modules
There are three types of modules:


 * Global modules - these are stored in dev.wikia.com, and can be either modules for templates, or helper modules. They are typically loaded using the syntax require("Dev:ModuleName") e.g. . Note that this is case sensitive.
 * Modules for templates - these modules must contain a frame argument in one of the functions.
 * Helper modules - these modules aren't intended for inclusion on pages but to be used by other modules.

Preview
The preview button allows one to see what the module will display before it is saved. It is a good idea always to work with two pages, one with the actual module being edited and one test page that invokes the module. Rather than refreshing the test page a simple preview will save time.

Parent or child arguments
One important concept is the difference between parent (use through a template) and child paremeters (using a module directly).The table below shows the differences. Read more about arguments here.

Incorrectly passed arguments
These can cause problems if they contain special characters such as. The result will be that the module may not correctly interpret the passed arguments if they are for example part of a URL string, e.g..

Passing arguments from one module to another
One of the greatest advantages of Lua is the ability to re-use existing code efficiently. So passing arguments to and from one module is important. There are several ways to achieve this:


 * Check if passed argument is a frame or regular object:


 * Create one function for the frame and a second for the arguments (recommended):


 * Use &mdash;this is not recommended because it is inefficient and fails to take advantage of powerful Lua methods:

Available frame API
Wikia doesn't always have access to the whole MediaWiki API, so it's best to be aware of the differences when designing a module. The following table shows which frame methods are currently available on Wikia, and provides links to the appropriate documentation.

Cache
One very important thing to remember when debugging a module is that wikia keeps a cache of  data. So changes made in modules may not always be immediately visible in a page. One way to get around this is to append  or   (e.g.  ) to all pages that are being debugged. See Help:purge for more information.

Debugging console

 * Debugging functions using the console - One can debug functions written in the module text area using the debug console. To do this one merely needs to call the function, e.g. . Note that functions need to be part of the main table, specifically , and functions outside this table cannot be called this way.
 * Console shows two types of errors - One of the error relates to a syntax problem in the console itself, and the other relates to errors in the module text area. Simply typing an expression will show one or both errors, e.g.:.
 * Print debug output - Output can be printed to the console using.
 * Frame - The frame is  (empty) using the debug console, as it is created during a module being used through #invoke.

Emulating the frame object using the console
A frame object is generally nil, but for the purposes of debugging on the console it can be emulated:

Testing modules
One way to test modules is by emulating the template call as shown above. This will make it easier to preview the output before saving the page and deploying the module.

Unit testing
There are several ways of testing modules/functions therein. One way to achieve this is by using Wikipedia's "UnitTests" framework.

Implementing modules
Modules can either be implemented and used as stand-alone using  or they can be used in conjunction with templates.

Working with templates or pages
While working with templates it is important to use the frame's parent, and to bear in mind that these modules may not work when called directly if they aren't coded properly:

Template:hello

output :Hello world output :

The code above will only work from a page that calls the "invoke" directly, however if calling from a template it will simply raise a syntax error. In order to make the above code work in templates and regular pages, a change will have to be made:

Template:hello

output :Hello world output :Hello world

The above code will work because it tests whether the frame arguments from either the template or the page have any value.

Choosing templates to convert
The best way to find out which templates need conversion is to view the pages below and improve the templates or the structure to reduce the parsing time (time it takes to load):


 * Special:ParserSpeed
 * Category:Pages with too many expensive parser function calls
 * Category:Pages where node count is exceeded
 * Category:Pages where template include size is exceeded
 * Category:Pages containing omitted template arguments
 * Category:Pages where expansion depth is exceeded

For an in-depth explanation see template limits.

Wikimedia modules
Wikimedia modules may either work as is when they are added to wikia or may require some tweaks (if they use a different scribunto extension).

Switch parser function
One way to quickly convert switch parser functions is to use a Lua module such as Module:Switch. This will greatly reduce the time it takes to make it Lua compatible and also fix the node count problem. Although it is advisable to eventually convert the tables to Lua.

Categorization
It is possible to categorize a module with wikitext, just like any other page, provided the category is not within  (a.k.a.  ), , or   tags.

Unlike other pages, categories are not listed at the bottom of Module: pages.