Fandom Developers Wiki
Tags: Visual edit apiedit
Tags: Visual edit apiedit
Line 38: Line 38:
 
<nowiki><pre>#invoke:modulename|functioname|input1|input2|input3|...}}</pre></nowiki>
 
<nowiki><pre>#invoke:modulename|functioname|input1|input2|input3|...}}</pre></nowiki>
   
Once the above code is executed a table (called frame) is created containing all those inputs, and it is passed to a function, and stored in a subtable called args (e.g. frame.args). For example, using<pre>{{#invoke:invocable|greet|john}}</pre> will call the module below:
+
Once the above code is executed a table (called frame) is created containing all those inputs, and it is passed to a function, and stored in a subtable called args (e.g. frame.args). For example, using the invoke below will make use of the module:<pre>{{#invoke:invocable|greet|john}}</pre><source lang="lua">
<source lang="lua">
 
 
--Table
 
--Table
 
local invocable = {}
 
local invocable = {}

Revision as of 11:57, 5 August 2015

This page covers some of the absolute basics for developing a simple lua template.

Basics

Before starting with lua templates, it is important to read up and get to know how to use regular wikitext templates, and preferably read/learn about lua in sites such as Wikibooks as well as reviewing Lua Reference manual.

Workspace

Lua templates are always stored in the Module namespace, and all work must always be saved there. For example, a module named helloworld would be stored in Module:Helloworld.

Creating a module

A module must always contain a table and a line containing a "return " for that table.

--Table
local p = {}
-- code goes here
return p

For a module to be invoked (or used in a page), it needs to have a function. However, this function must be part of the main table (e.g. invocable):

--Table
local invocable = {}

--can be invoked
function invocable.greet(frame)
   return "Live long and prosper"
end

-- can't be invoked 
function askname(frame)
   return "What's your name?"
end
return invocable

Using input (parameters)

Input can be provided to a module during the invoke:

<pre>#invoke:modulename|functioname|input1|input2|input3|...}}</pre>

Once the above code is executed a table (called frame) is created containing all those inputs, and it is passed to a function, and stored in a subtable called args (e.g. frame.args). For example, using the invoke below will make use of the module:

{{#invoke:invocable|greet|john}}
--Table
local invocable = {}

function invocable.greet(frame)
   local name = frame.args[1]
   return "Live long and prosper " ..name
end

return invocable
Output : Live long and prosper john

Accessing template input

However, the invoke above cannot be used through a template because the values from a page, and a sub-page are kept separate. Those arguments can be accessed from a template by first retrieving the parent frame or table containing the arguments (i.e. frame.getParent()), and then using the sub-table (frame:getParent().args):

  • Template:greet
--Table
local invocable = {}

function invocable.greet(frame)
   local parent = frame.getParent()
   local name = parent.args[1]
   return "Live long and prosper " ..name
end

return invocable
{{greet|Jack}}
Output:Live long and prosper Jack 

Debug console

See Debug console.

Advanced

Using other modules

Using external tables

See also