Fandom Developers Wiki
(→‎See also: X links)
Tags: Visual edit apiedit
m (→‎Meta-Modules: Correct link)
Tag: Visual edit
(16 intermediate revisions by 8 users not shown)
Line 1: Line 1:
  +
{{Infobox Lua templating}}
[[File:Lua-logo-nolabel.png|thumb|220x220px]]
 
This page covers some of the absolute basics for developing a simple [[lua]] template.
+
This page covers some of the absolute basics for developing a simple [[Help:Lua|Lua]] template.
   
 
== Basics ==
 
== Basics ==
  +
{{Main|Lua templating/Basics}}
Before starting with lua templates, it is important to read up and get to know how to use regular [[Help:Templates|wikitext templates]], and preferably read/learn about lua in sites such as [[Wikibooks:Lua Programming|Wikibooks]] as well as reviewing [[Lua templating/Reference manual|Lua Reference manual]].
+
Before starting with lua templates, it is important to read up and get to know how to use regular [[Help:Templates|wikitext templates]], and preferably read/learn about lua in sites such as [[Wikibooks:Lua Programming|Wikibooks]] as well as reviewing the [[Lua templating/Reference manual|Lua Reference manual]].
   
 
=== Workspace ===
 
=== Workspace ===
Lua templates are always stored in the [[Help:namespaces|Module namespace]], and all work must always be saved there. For example, a module named helloworld would be stored in Module:Helloworld.
+
Lua templates are stored in the [[Help:namespaces|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 ===
 
=== Creating a module ===
A module must always contain a [[Wikibooks:Lua Programming/Tables|table]] and a line containing a "return " for that table unless  it is a meta-module(see below). 
+
A module must always contain a [[Wikibooks:Lua Programming/Tables|table]] and a line containing a "return " for that table unless  it is a meta-module (see below). 
   
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
--Table
 
--Table
 
local p = {}
 
local p = {}
 
-- code goes here
 
-- code goes here
 
return p
 
return p
</source>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):
+
</syntaxhighlight>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):
   
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
--Table
 
--Table
 
local invocable = {}
 
local invocable = {}
Line 32: Line 33:
 
end
 
end
 
return invocable
 
return invocable
  +
</syntaxhighlight>
</source>
 
   
 
=== Execution time ===
 
=== Execution time ===
Lua templates/modules in a page have a maximum time for execution currently set at 10 seconds. This means that lua modules within a page cannot take longer than 10 seconds to execute, or there will be an error.
+
Lua modules used can only run for a maximum of 10 seconds. This means that modules within a page cannot take longer than 10 seconds to execute, or there will be an error.
==The frame==
 
   
  +
== Copying modules to another wiki ==
=== Description ===
 
  +
Modules hosted here can be used in another wiki, but this may require steps:
  +
# Copy the module to your wiki, e.g. [[Module:Links]]
  +
# Copy all modules it depends on to your wiki. This is a bit complicated because Fandom uses an older scribunto version. But these steps should help:
  +
## Open the module page and find sections that contain text like "require("text"), e.g. in module links text =="Dev:Arguments".
  +
## Search for the module in dev.wikia or any other wiki, e.g. Module:Arguments
  +
## Copy this module:arguments to your wiki
  +
## Replace all mentions of require("Dev:") with require("module:"), e.g. require("Module:Arguments")
  +
# Redo step two, for every time a require("") is found in copied module.
   
 
==Using input (parameters)==
=== Purpose ===
 
 
=== Use ===
 
 
== Using input (parameters) ==
 
 
Input (or [[Help:Template parameters|Template parameters]]) can be provided to a module during the invoke.
 
Input (or [[Help:Template parameters|Template parameters]]) can be provided to a module during the invoke.
   
Line 50: Line 54:
 
<pre>{{#invoke:modulename|functioname|input1|input2|input3|...}}</pre>
 
<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:
+
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 sub-table called args (e.g. frame.args). For example, using the invoke below will make use of the module:
 
<pre>{{#invoke:invocable|greet|john}}</pre>
 
<pre>{{#invoke:invocable|greet|john}}</pre>
 
<pre>{{#invoke:invocable|greet|name=john}}</pre>
 
<pre>{{#invoke:invocable|greet|name=john}}</pre>
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
--Module:Invocable
 
--Module:Invocable
 
--Table
 
--Table
Line 65: Line 69:
   
 
return invocable
 
return invocable
  +
</syntaxhighlight>
</source>
 
 
<pre>Output : Live long and prosper john</pre>
 
<pre>Output : Live long and prosper john</pre>
   
Line 78: Line 82:
   
 
'''Module:Invocable:'''
 
'''Module:Invocable:'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
--Table
 
--Table
 
local invocable = {}
 
local invocable = {}
Line 91: Line 95:
   
 
return invocable
 
return invocable
  +
</syntaxhighlight>
</source>
 
 
'''Usage:'''
 
'''Usage:'''
 
<pre>{{greet|Jack}}
 
<pre>{{greet|Jack}}
Line 137: Line 141:
   
 
== Script errors ==
 
== Script errors ==
Whenever a module doesn't work properly it triggers a script error in a page. A good explanation of script errors is maintained by Wikipedia<ref>[[Wikipedia:Lua_error_messages]]</ref>.
+
Whenever a module doesn't work properly it triggers a script error in a page. A good explanation of script errors is maintained by Wikipedia<ref>[[Wikipedia:Wikipedia:Lua error messages]]</ref>.
 
=== Debug console ===
 
See [[Lua templating/Debug console|Debug console]].
 
   
 
== Advanced ==
 
== Advanced ==
The advantage of lua is that it enables one to use external modules and tables that have been created by others. This reduces the need to reinvent the wheel, and more time can actually be spent creating new solutions.
+
An advantage of lua is that it enables one to use external modules and tables that have been created by others. This reduces the need to reinvent the wheel, and more time can actually be spent creating new solutions.
   
 
=== Using other modules ===
 
=== Using other modules ===
 
To use libraries or modules one needs to import that library. This is done using the [[Lua templating/Reference manual/Standard libraries|require]] method:
 
To use libraries or modules one needs to import that library. This is done using the [[Lua templating/Reference manual/Standard libraries|require]] method:
   
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
--Module:Libraries
 
--Module:Libraries
 
local library = {}
 
local library = {}
Line 159: Line 160:
   
 
return library
 
return library
  +
</syntaxhighlight>
</source>
 
 
<pre>{{#invoke:library|greet|Zeus}}
 
<pre>{{#invoke:library|greet|Zeus}}
 
Output:Live long and prosper Zeus
 
Output:Live long and prosper Zeus
Line 172: Line 173:
 
A external table can be retrieved in the same way as an external module, except that there is an extra method ([[Lua templating/Reference manual/Scribunto libraries|mw.loadData]]) that loads it once per page, making it more efficient.
 
A external table can be retrieved in the same way as an external module, except that there is an extra method ([[Lua templating/Reference manual/Scribunto libraries|mw.loadData]]) that loads it once per page, making it more efficient.
   
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
--Module:Tables
 
--Module:Tables
 
local tables = {'food','garden','relic'}
 
local tables = {'food','garden','relic'}
   
 
return tables
 
return tables
  +
</syntaxhighlight>
</source>
 
   
 
==== Usage: ====
 
==== Usage: ====
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
--Module:showobjects
 
--Module:showobjects
 
local p = {}
 
local p = {}
Line 193: Line 194:
   
 
return p
 
return p
  +
</syntaxhighlight>
</source>
 
 
<pre>{{#invoke:showobjects|show}}
 
<pre>{{#invoke:showobjects|show}}
 
Output: "food & garden"
 
Output: "food & garden"
Line 199: Line 200:
   
 
=== Global modules ===
 
=== Global modules ===
Modules stored in Dev.wikia.com are called global modules. They work in a similar manner to modules stored in a wikia but they can be accessed by any lua module from another wikia(e.g. food.wikia.com). The difference lies only in the syntax (it uses "Dev" instead of "Module") used to obtain the modules:
+
Modules stored in dev.fandom.com are called global modules. They work in a similar manner to modules stored in a wiki but can be accessed by any lua module from another wiki (e.g. food.fandom.com). The difference lies only in the syntax (it uses "Dev" instead of "Module") used to obtain the modules:
   
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
local global_invocable = require("Dev:Invocable")
 
local global_invocable = require("Dev:Invocable")
 
local global_Tables = mw.loadData("Dev:Tables")
 
local global_Tables = mw.loadData("Dev:Tables")
  +
</syntaxhighlight>
</source>
 
  +
 
== Tools ==
  +
There are a bunch of useful tools that can help create modules:
  +
 
=== Code-editor ===
  +
The code-editor (or ace-editor) - this is the default editor that can be disabled as needed.
  +
  +
Ace editor in particular comes with a couple of hidden features such as keyboard shortcuts and macros<ref>https://github.com/ajaxorg/ace/wiki/Default-Keyboard-Shortcuts</ref>.
  +
  +
==== Keyboard shortcuts ====
  +
A brief list of some useful shortcuts is shown below:
  +
{|
  +
!Windows/Linux
  +
!Mac
  +
!Action
  +
|-
  +
|Alt-Shift-Down
  +
|Command-Option-Down
  +
|Copy lines down
  +
|-
  +
|Alt-Shift-Up
  +
|Command-Option-Up
  +
|Copy lines up
  +
|-
  +
|Alt-Down
  +
|Option-Down
  +
|Move lines down
  +
|-
  +
|Alt-Up
  +
|Option-Up
  +
|Move lines up
  +
|-
  +
|Alt-Delete
  +
|Ctrl-K
  +
|Remove to line end
  +
|-
  +
|Alt-Backspace
  +
|Command-Backspace
  +
|Remove to linestart
  +
|-
  +
|Ctrl-Backspace
  +
|Option-Backspace, Ctrl-Option-Backspace
  +
|Remove word left
  +
|-
  +
|Ctrl-Delete
  +
|Option-Delete
  +
|Remove word right
  +
|-
  +
|<nowiki>---</nowiki>
  +
|Ctrl-O
  +
|Split line
  +
|}
  +
{|
  +
!Windows/Linux
  +
!Mac
  +
!Action
  +
|-
  +
|Ctrl-Shift-E
  +
|Command-Shift-E
  +
|Macros replay
  +
|-
  +
|Ctrl-Alt-E
  +
|<nowiki>---</nowiki>
  +
|Macros recording
  +
|}
  +
  +
=== Syntax highlighting and syntax checking ===
  +
The interactive code editor always highlights syntax errors, and sometimes gives helpful information to fix them.
  +
 
=== Debug console ===
 
{{Main|Lua templating/Debug console}}
  +
This is a console or terminal that makes it easy to test out code.
   
 
== See also ==
 
== See also ==
* [[Lua templating]]
+
* [[Help:Lua]]
 
* [[Lua templating/Debug console]]
 
* [[Lua templating/Debug console]]
 
* [[Lua templating/Glossary]]
 
* [[Lua templating/Glossary]]
Line 214: Line 287:
 
== References ==
 
== References ==
 
<references />
 
<references />
  +
[[Category:Lua]]
+
[[Category:Lua|Getting started]]

Revision as of 22:50, 1 August 2020

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

Basics

Main article: Lua templating/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 the Lua Reference manual.

Workspace

Lua templates are 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 unless  it is a meta-module (see below). 

--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

Execution time

Lua modules used can only run for a maximum of 10 seconds. This means that modules within a page cannot take longer than 10 seconds to execute, or there will be an error.

Copying modules to another wiki

Modules hosted here can be used in another wiki, but this may require steps:

  1. Copy the module to your wiki, e.g. Module:Links
  2. Copy all modules it depends on to your wiki. This is a bit complicated because Fandom uses an older scribunto version. But these steps should help:
    1. Open the module page and find sections that contain text like "require("text"), e.g. in module links text =="Dev:Arguments".
    2. Search for the module in dev.wikia or any other wiki, e.g. Module:Arguments
    3. Copy this module:arguments to your wiki
    4. Replace all mentions of require("Dev:") with require("module:"), e.g. require("Module:Arguments")
  3. Redo step two, for every time a require("") is found in copied module.

Using input (parameters)

Input (or Template parameters) can be provided to a module during the invoke.

Syntax

{{#invoke:modulename|functioname|input1|input2|input3|...}}

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 sub-table called args (e.g. frame.args). For example, using the invoke below will make use of the module:

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

function invocable.greet(frame)
   local name = frame.args[1] or frame.args["name"]

   return "Live long and prosper " ..name
end

return invocable
Output : Live long and prosper john

Explanation : Args is a list containing all parameters, args[1] refers to the first value, e.g. john, args["name"] accesses a named parameter "name".

Accessing template input

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 only 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:

{{#invoke:invocable|greet}}

Module:Invocable:

--Table
local invocable = {}

function invocable.greet(frame)
   local parent = frame:getParent()
   local name = parent.args[1]
   local name2 = parent.args[2] or ""

   return "Live long and prosper :" ..name ..' '..name2
end

return invocable

Usage:

{{greet|Jack}}
Output:Live long and prosper : Jack 
{{greet|Jack|Jill}} 
Output:Live long and prosper : Jack  Jill
Example Parameter Template Module Output
{{greet|john}} 1 {{{1}}} frame:getParent().args[1] john
{{greet|name=Spock}} name {{{name}}} frame:getParent().args["name"] Spock
{{#invoke:invocable|greet|john}} 1 frame.args[1] john
{{#invoke:invocable|greet|name=Worf}} name frame.args["name"] Worf
{{#invoke:invocable|greet|jack|jill}} 1,2 frame.args[1], frame.args[2] Jack , Jill

Script errors

Whenever a module doesn't work properly it triggers a script error in a page. A good explanation of script errors is maintained by Wikipedia[1].

Advanced

An advantage of lua is that it enables one to use external modules and tables that have been created by others. This reduces the need to reinvent the wheel, and more time can actually be spent creating new solutions.

Using other modules

To use libraries or modules one needs to import that library. This is done using the require method:

--Module:Libraries
local library = {}

function library.greet(frame)
   local invocable = require("Module:Invocable")

   return invocable.greet(frame)
end

return library
{{#invoke:library|greet|Zeus}}
Output:Live long and prosper Zeus


Note: The syntax is case sensitive so "Dev" != "dev".

Meta-Modules

These are modules that are not meant to be used in a page (e.g. {{#invoke), and don't necessarily have any functions that can be used in a page.

Using external tables

A external table can be retrieved in the same way as an external module, except that there is an extra method (mw.loadData) that loads it once per page, making it more efficient.

--Module:Tables
local tables = {'food','garden','relic'}

return tables

Usage:

--Module:showobjects
local p = {}

function p.show(frame)
   local objects = require("Module:Tables")
   --using loadData
   local objects2 = mw.loadData("Module:Tables")

   return objects[1] ..' & ' objects2[2]
end

return p
{{#invoke:showobjects|show}}
Output: "food & garden"

Global modules

Modules stored in dev.fandom.com are called global modules. They work in a similar manner to modules stored in a wiki but can be accessed by any lua module from another wiki (e.g. food.fandom.com). The difference lies only in the syntax (it uses "Dev" instead of "Module") used to obtain the modules:

local global_invocable = require("Dev:Invocable")
local global_Tables = mw.loadData("Dev:Tables")

Tools

There are a bunch of useful tools that can help create modules:

Code-editor

The code-editor (or ace-editor) - this is the default editor that can be disabled as needed.

Ace editor in particular comes with a couple of hidden features such as keyboard shortcuts and macros[2].

Keyboard shortcuts

A brief list of some useful shortcuts is shown below:

Windows/Linux Mac Action
Alt-Shift-Down Command-Option-Down Copy lines down
Alt-Shift-Up Command-Option-Up Copy lines up
Alt-Down Option-Down Move lines down
Alt-Up Option-Up Move lines up
Alt-Delete Ctrl-K Remove to line end
Alt-Backspace Command-Backspace Remove to linestart
Ctrl-Backspace Option-Backspace, Ctrl-Option-Backspace Remove word left
Ctrl-Delete Option-Delete Remove word right
--- Ctrl-O Split line
Windows/Linux Mac Action
Ctrl-Shift-E Command-Shift-E Macros replay
Ctrl-Alt-E --- Macros recording

Syntax highlighting and syntax checking

The interactive code editor always highlights syntax errors, and sometimes gives helpful information to fix them.

Debug console

Main article: Lua templating/Debug console

This is a console or terminal that makes it easy to test out code.

See also

References