Lua templating/InfoboxBuilder

What's new?
We are happy to introduce a completely new Infobox Builder! It makes creating templates for Infoboxes much easier and faster. You will also benefit from faster rendering of pages.

What do I need?
Copy these modules: Simply create pages with matching names on your wikia and copy the content.
 * Module:InfoboxBuilder
 * Module:InfoboxBuilderView
 * Module:HF

Step 1 - Provide data
If you convert an old wikitext template to Lua you probably have some articles that provide you the data. So, good news! You don't have to change anything! Just update your template and the rest will happen.

However, if you write a new template you need some input. In this example we will use this wikitext code:

You can find this code in the beginning of this article.

It calls a Template:LuaInfobox article and passes some info about the InfoboxBuilder module.

Step 2 - Infobox Template
We are ready to go! At first, we will create a template to model our infobox. To be consistent with the data from Step 1 let's call it Template:LuaInfobox.

Invoking InfoboxBuilder module
We will get to actual building in a second. Let's start with a necessary structure there:

The new thing here is the "#invoke" command which is used to call Lua modules in wikitext templates.

The syntax
Let's build the infobox you can see in this article using the new syntax that InfoboxBuilder uses:

The general pattern for each argument is: | [Index]:[Key] = [Value]

Variables
Arguments with a 0 index are general variables. The always present ones are:

Theme                  = "default", -- Adds prefix to CSS classes CustomModule           = " ",       -- Defines a path to a module with custom functions MainImageCaption       = "Off",     -- Toggles display of Label in the Main Image field ToggleContentLongerThan = 1000      -- Makes fields with long values collapsible

You can also define your own variables, e.g. if you want to use a magic word add: The pagename of an article using this template will be passed now for a later use.
 * 0:Pagename =

Fields
Fields are blocks of which an infobox is made. Its basic definition consists of Type, Label and Value. Types that are available for now are:


 * Title
 * Header
 * Line
 * Image
 * MainImage
 * Link
 * Footer

Each element has its own CSS class so you can style them differently. They also have different HTML markup. Type, Label and Value are assigned to one field by having the same numeric index.

Label is displayed by default for Line and Image and you can turn in on for MainImage. For other fields it is has an informative and descriptive role.

The default behavior for Line type blocks is that it is not displayed if its Value is empty. That's why it is important to use a | (pipe) to pass an empty string if no value is provided in an article.

But this is quite boring. Let's make our infobox a little bit more fancy!

Step 3 - Custom modules
If you want to process a Label or a Value of a field you can include your own Lua module by passing its full name (with a Module: prefix) in a CustomModule variable e.g.:

{{#invoke:InfoboxBuilder | builder |  0:Theme        = default |  0:CustomModule = Module:LuaInfobox

If you want to read more on developing Lua modules - check out the reference manual. As an example let's write a module with a method that changes a name of each module in Dependencies into a link:

Custom module's code
You can find the source here: Module:LuaInfobox

local HF = require('Module:HF')

local LuaInfobox = {}

function LuaInfobox.Dependencies( field, vars ) local output = ""

local dependencies = HF.explode( ";", field.Value )

for i, dependency in ipairs( dependencies ) do   output = output .. "" .. dependency .. "" .. "; " end

if string.sub( output, -2, -1 ) == "; " then output = string.sub( output, 1, -3 ) -- Remove last semi-colon and space end

return output end

return LuaInfobox

Every method retrieves two arguments - table of a field with names of keys matching the ones from a template (e.g. Type, Label, Value) and a similar table of variables (e.g. Theme, Pagename).

As you can see you are able to require other Lua modules so you don't have to store everything in one place. Be careful however, because Lua's engine has a limit of 100 modules included at once.

Usage in a template
Now we need to instruct InfoboxBuilder to use this method for a Dependency field and it is done by adding a ValueMethod argument with a function's name:

|  5:Type        = Line | 5:Label      = Dependencies | 5:Value      = | 5:ValueMethod = Dependencies

There is no naming convention to stick to, however using a module's name for a main variable and fields' names for functions will help you keep things organised. You can also process Labels in the same way. Just add LabelMethod argument with a name of a function you want to use.

Multiple values
Since every method has access to all arguments grouped in one field you can pass multiple values to it. For example - we want to point that all dependencies are required. We can do it by adding a new argument to the Dependencies field:

|  5:Type        = Line | 5:Label      = Dependencies | 5:Value      = | 5:ValueDesc  = | 5:ValueMethod = Dependencies

and define its value in an article:


 * Dependencies    = InfoboxBuilderView; HF
 * DependenciesDesc = (all are required)

Now we can use it to add additional notes to the Dependencies field:

function LuaInfobox.Dependencies( field, vars ) local output = ""

local dependencies = HF.explode( ";", field.Value )

for i, dependency in ipairs( dependencies ) do   output = output .. "" .. dependency .. "" .. "; " end

if string.sub( output, -2, -1 ) == "; " then output = string.sub( output, 1, -3 ) -- Remove last semi-colon and space end

if not HF.isempty( field.ValueDesc ) then output = output .. " " .. field.ValueDesc end

return output end

Step 4 - Get creative!
Building infoboxes using custom Lua modules gives you a lot more possibilities than wikitext templates alone. Check out the articles available in Category:Lua and... get creative!