Fandom Developers Wiki
(wikia > fandom)
mNo edit summary
 
(22 intermediate revisions by 10 users not shown)
Line 1: Line 1:
  +
<noinclude>{{LangSelect}}</noinclude><includeonly>{{Languages}}
{{Infobox Lua templating}}
+
{{Infobox Lua templating
This page covers some of the absolute basics for developing a simple [[Help:Lua|Lua]] template.
 
  +
|Article = {{l10n|Infobox Lua templating|Getting-started}}
  +
}}
 
{{{intro|This page covers some of the absolute basics for developing a simple [[w:Help:Lua|Lua]] template.}}}
   
== Basics ==
+
== {{{basics-header|Basics}}} ==
{{Main|Lua templating/Basics}}
+
{{Main|Lua templating/Basics|{{l10n|Infobox 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 the [[Lua templating/Reference manual|Lua Reference manual]].
+
{{{basics-instructions|Before starting with Lua templates, it is important to read up and get to know how to use regular [[w: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-header|Workspace}}} ===
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.
+
{{{workspace-instructions|Lua templates are stored in the [[w: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 ===
+
=== {{{create-header|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). 
+
{{{create-instructions|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-Modules|meta-module]] (see below).}}}
   
<syntaxhighlight lang="lua">
+
{{#tag:syntaxhighlight|
--Table
+
--{{{table|Table}}}
 
local p = {}
 
local p = {}
-- code goes here
+
-- {{{code-here|code goes here}}}
 
return p
 
return p
  +
|lang="lua"
</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):
 
  +
}}
 
{{{table-note|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 lang="lua">
+
{{#tag:syntaxhighlight|
--Table
+
--{{{table|Table}}}
local invocable = {}
+
local {{{invocable|invocable}}} = {}
   
--can be invoked
+
--{{{cbi|can be invoked}}}
function invocable.greet(frame)
+
function {{{invocable|invocable}}}.{{{greet|greet}}}(frame)
return "Live long and prosper"
+
return "{{{llap|Live long and prosper}}}"
 
end
 
end
   
-- can't be invoked
+
--{{{cant-invoke|can't be invoked}}}
function askname(frame)
+
function {{{askname|askname}}}(frame)
return "What's your name?"
+
return "{{{wyn|What's your name?}}}"
 
end
 
end
return invocable
+
return {{{invocable|invocable}}}
  +
|lang="lua"
</syntaxhighlight>
 
  +
}}
   
=== Execution time ===
+
=== {{{execution-header|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.
+
{{{execution-instructions|Lua modules used can only run for a maximum of 7 seconds. This means that modules within a page cannot take longer than 7 seconds to execute, or there will be an error.}}}
   
== Copying modules to another wiki ==
+
== {{{copying-header|Copying modules to another wiki}}} ==
Modules hosted here can be used in another wiki, but this may require steps:
+
{{{copying-instructions|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 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:
+
# 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".
+
## Open the module page and find sections that contain text like <code>require("text")</code>, e.g. in module links text <code>=="Dev:Arguments"</code>.
## Search for the module in dev.wikia or any other wiki, e.g. Module:Arguments
+
## Search for the module in <code>dev.fandom</code> or any other wiki, e.g. Module:Arguments
## Copy this module:arguments to your wiki
+
## Copy this Module:Arguments to your wiki
## Replace all mentions of require("Dev:") with require("module:"), e.g. require("Module:Arguments")
+
## Replace all mentions of <code>require("Dev:")</code> with <code>require("module:")</code>, e.g. <code>require("Module:Arguments")</code>
# Redo step two, for every time a require("") is found in copied module.
+
# Redo step two, for every time a <code>require("")</code> is found in copied module.
  +
}}}
 
== {{{input-header|Using input (parameters)}}} ==
 
{{{input-instructions|Input (or [[w:Help:Template parameters|Template parameters]]) can be provided to a module during the invoke.)}}}
   
 
==== {{{syntax-header|Syntax}}} ====
==Using input (parameters)==
 
  +
{{#tag:pre|
Input (or [[Help:Template parameters|Template parameters]]) can be provided to a module during the invoke.
 
  +
{<nowiki/>{#invoke:{{{module-name|modulename}}}<nowiki>|</nowiki>{{{function-name|functioname}}}<nowiki>|</nowiki>{{{input1|input1}}}<nowiki>|</nowiki>{{{input2|input2}}}<nowiki>|</nowiki>{{{input3|input3}}}<nowiki>|</nowiki>...}<nowiki/>}
  +
}}
   
 
{{{syntax-instructions|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. <code>frame.args</code>). For example, using the invoke below will make use of the module:}}}
==== Syntax ====
 
  +
{{#tag:pre|
<pre>{{#invoke:modulename|functioname|input1|input2|input3|...}}</pre>
 
  +
{<nowiki/>{#invoke:{{{invocable|invocable}}}<nowiki>|</nowiki>{{{greet|greet}}}<nowiki>|</nowiki>john}<nowiki/>}
  +
}}
  +
{{#tag:pre|
  +
{<nowiki/>{#invoke:{{{invocable|invocable}}}<nowiki>|</nowiki>{{{greet|greet}}}<nowiki>|</nowiki>{{{name|name}}}=john}<nowiki/>}
  +
}}
 
{{#tag:syntaxhighlight|
  +
--{{{module|Module}}}:{{{invocable2|Invocable}}}
  +
--{{{table|Table}}}
 
local {{{invocable|invocable}}} = {}
   
 
function {{{invocable|invocable}}}.{{{greet|greet}}}(frame)
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:
 
 
local {{{name|name}}} = frame.args[1] or frame.args["{{{name|name}}}"]
<pre>{{#invoke:invocable|greet|john}}</pre>
 
<pre>{{#invoke:invocable|greet|name=john}}</pre>
 
<syntaxhighlight lang="lua">
 
--Module:Invocable
 
--Table
 
local invocable = {}
 
 
function invocable.greet(frame)
 
local name = frame.args[1] or frame.args["name"]
 
   
return "Live long and prosper " ..name
+
return "{{{llap|Live long and prosper}}} " ..{{{name|name}}}
 
end
 
end
   
return invocable
+
return {{{invocable|invocable}}}
  +
|lang="lua"
</syntaxhighlight>
 
  +
}}
<pre>Output : Live long and prosper john</pre>
 
  +
;{{{output|Output}}}
  +
{{#tag:pre|
 
{{{llap-output|Live long and prosper john}}}
  +
}}
   
Explanation :
+
{{{syntax-explanation|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".
+
Args is a list containing all parameters, <code>args[1]</code> refers to the first value, e.g. john, <code>args["name"]</code> accesses a named parameter "name".}}}
   
=== Accessing template input ===
+
=== {{{access-header|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):
+
{{{access-instructions|The code in the module above, if wrapped in a wikitext template, cannot access the parameters passed to the template. Such arguments can be accessed by first retrieving the parent frame using <code>frame:getParent()</code>, and then accessing the sub-table <code>frame:getParent().args</code>:}}}
   
  +
; {{ns:Template}}<nowiki>:</nowiki>{{{greet|greet}}}
'''Template:Greet:'''
 
  +
{{#tag:pre|
<pre>{{#invoke:invocable|greet}}</pre>
 
  +
{<nowiki/>{#invoke:{{{invocable|invocable}}}<nowiki>|</nowiki>{{{greet|greet}}}}<nowiki/>}
  +
}}
   
  +
; {{ns:Module}}<nowiki>:</nowiki>{{{invocable|invocable}}}
'''Module:Invocable:'''
 
<syntaxhighlight lang="lua">
+
{{#tag:syntaxhighlight|
--Table
+
--{{{table|Table}}}
local invocable = {}
+
local {{{invocable|invocable}}} = {}
   
function invocable.greet(frame)
+
function {{{invocable|invocable}}}.{{{greet|greet}}}(frame)
 
local parent = frame:getParent()
 
local parent = frame:getParent()
local name = parent.args[1]
+
local {{{name|name}}} = parent.args[1]
local name2 = parent.args[2] or ""
+
local {{{name2|name2}}} = parent.args[2] or ""
   
return "Live long and prosper :" ..name ..' '..name2
+
return "{{{llap2|Live long and prosper :}}}" ..{{{name|name}}} ..' '..{{{name2|name2}}}
 
end
 
end
   
return invocable
+
return {{{invocable|invocable}}}
  +
|lang="lua"
</syntaxhighlight>
 
  +
}}
'''Usage:'''
 
<pre>{{greet|Jack}}
+
;{{{usage|Usage}}}
  +
{{#tag:pre|
Output:Live long and prosper : Jack
 
  +
{<nowiki/>{{{{greet|greet}}}<nowiki>|</nowiki>Jack}<nowiki/>}
{{greet|Jack|Jill}}
 
  +
}}
Output:Live long and prosper : Jack Jill</pre>
 
  +
;{{{output|Output}}}
  +
{{#tag:pre|
 
{{{llap2|Live long and prosper :}}}Jack
  +
}}
  +
;{{{usage|Usage}}}
  +
{{#tag:pre|
  +
{<nowiki/>{{{{greet|greet}}}<nowiki>|</nowiki>Jack<nowiki>|</nowiki>Jill}<nowiki/>}
  +
}}
  +
;{{{output|Output}}}
  +
{{#tag:pre|
 
{{{llap2|Live long and prosper :}}}Jack Jill
  +
}}
   
 
{| class="wikitable"
 
{| class="wikitable"
!Example
+
!{{{example|Example}}}
!Parameter
+
!{{{parameter|Parameter}}}
!Template
+
!{{{template|Template}}}
!Module
+
!{{{module|Module}}}
!Output
+
!{{{output|Output}}}
 
|-
 
|-
|<nowiki>{{greet|john}}</nowiki>
+
|<nowiki>{{</nowiki>{{{greet|greet}}}<nowiki>|</nowiki>john}}
 
|1
 
|1
| {{{1}}}
+
| <nowiki>{{{1}}}</nowiki>
 
|frame:getParent().args[1]
 
|frame:getParent().args[1]
 
|john
 
|john
 
|-
 
|-
|<nowiki>{{greet|name=Spock}}</nowiki>
+
|<nowiki>{{</nowiki>{{{greet|greet}}}<nowiki>|</nowiki>{{{name|name}}}=Spock}}
|name
+
|{{{name|name}}}
  +
|<nowiki>{{{</nowiki>{{{name|name}}}<nowiki>}}}}</nowiki>
|{{{name}}}
 
|frame:getParent().args["name"]
+
|frame:getParent().args["{{{name|name}}}"]
 
|Spock
 
|Spock
 
|-
 
|-
|<nowiki>{{#invoke:invocable|greet|john}}</nowiki>
+
|{<nowiki/>{#invoke:{{{invocable|invocable}}}<nowiki>|</nowiki>{{{greet|greet}}}<nowiki>|</nowiki>john}}
 
|1
 
|1
 
|
 
|
Line 127: Line 157:
 
|john
 
|john
 
|-
 
|-
|<nowiki>{{#invoke:invocable|greet|name=Worf}}</nowiki>
+
|{<nowiki/>{#invoke:{{{invocable|invocable}}}<nowiki>|</nowiki>{{{greet|greet}}}<nowiki>|</nowiki>{{{name|name}}}=Worf}}
|name
+
|{{{name|name}}}
 
|
 
|
|frame.args["name"]
+
|frame.args["{{{name|name}}}"]
 
|Worf
 
|Worf
 
|-
 
|-
|<nowiki>{{#invoke:invocable|greet|jack|jill}}</nowiki>
+
|{<nowiki/>{#invoke:{{{invocable|invocable}}}<nowiki>|</nowiki>{{{greet|greet}}}<nowiki>|</nowiki>jack<nowiki>|</nowiki>jill}}
 
|1,2
 
|1,2
 
|
 
|
Line 140: Line 170:
 
|}
 
|}
   
== Script errors ==
+
== {{{errors-header|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>.
+
{{{errors-instructions|Whenever a module doesn't work properly it triggers a script error in a page. A [[wikipedia:Wikipedia:Lua error messages|good explanation of script errors]] is maintained by Wikipedia.}}}
   
== Advanced ==
+
== {{{advanced-header|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.
+
{{{advanced-instructions|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 ===
+
=== {{{other-header|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:
+
{{{other-instructions|To use libraries or modules one needs to import that library. This is done using the [[Lua templating/Reference manual/Standard libraries|require]] method:}}}
   
<syntaxhighlight lang="lua">
+
{{#tag:syntaxhighlight|
--Module:Libraries
+
--{{{module|Module}}}:Libraries
local library = {}
+
local {{{library|library}}} = {}
   
function library.greet(frame)
+
function {{{library|library}}}.{{{greet|greet}}}(frame)
local invocable = require("Module:Invocable")
+
local {{{invocable|invocable}}} = require("{{{module|Module}}}:{{{invocable|invocable}}}")
   
return invocable.greet(frame)
+
return {{{invocable|invocable}}}.{{{greet|greet}}}(frame)
 
end
 
end
   
return library
+
return {{{library|library}}}
  +
|lang="lua"
</syntaxhighlight>
 
  +
}}
<pre>{{#invoke:library|greet|Zeus}}
 
  +
{{#tag:pre|
Output:Live long and prosper Zeus
 
  +
{<nowiki/>{#invoke:{{{library|library}}}<nowiki>|</nowiki>{{{greet|greet}}}<nowiki>|</nowiki>Zeus}<nowiki/>}
</pre>
 
  +
}}
  +
;{{{output|Output}}}
  +
{{#tag:pre|
 
{{{llapz|Live long and prosper Zeus}}}
  +
}}
   
 
{{{dev-note|'''Note:''' The syntax is [[wikipedia:Case sensitivity|case sensitive]] so <code>"Dev" != "dev"</code>.}}}
   
 
==== {{{meta-header|Meta-Modules}}} ====
'''Note:''' The syntax is [[Wikipedia:case sensitivity|case sensitive]] so "Dev" != "dev".
 
 
{{{meta-instructions|These are modules that are not meant to be used in a page (e.g. <nowiki>{{#invoke</nowiki>), and don't necessarily have any functions that can be used in a page.}}}
====Meta-Modules====
 
These are modules that are not meant to be used in a page (e.g. <nowiki>{{#invoke</nowiki>), and don't necessarily have any functions that can be used in a page.
 
   
=== Using external tables ===
+
=== {{{external-header|Using external tables}}} ===
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.
+
{{{external-instructions|A external table can be retrieved in the same way as an external module, except that there is an extra method (<code>[[Lua templating/Reference manual/Scribunto libraries#mw.loadData|mw.loadData]]</code>) that loads it once per page, making it more efficient.}}}
 
{{#tag:syntaxhighlight|
 
--{{{module|Module}}}:Tables
 
local {{{tables|tables}}} = {'{{{food|food}}}','{{{garden|garden}}}','{{{relic|relic}}}'}
 
return {{{tables|tables}}}
  +
|lang="lua"
  +
}}
   
  +
;{{{usage|Usage}}}
<syntaxhighlight lang="lua">
 
 
{{#tag:syntaxhighlight|
--Module:Tables
 
 
--{{{module|Module}}}:showobjects
local tables = {'food','garden','relic'}
 
 
return tables
 
</syntaxhighlight>
 
 
==== Usage: ====
 
<syntaxhighlight lang="lua">
 
--Module:showobjects
 
 
local p = {}
 
local p = {}
   
function p.show(frame)
+
function p.{{{show|show}}}(frame)
local objects = require("Module:Tables")
+
local {{{objects|objects}}} = require("{{{module|Module}}}:Tables")
--using loadData
+
--{{{using-ld|using loadData}}}
local objects2 = mw.loadData("Module:Tables")
+
local {{{objects2|objects2}}} = mw.loadData("{{{module|Module}}}:Tables")
   
return objects[1] ..' & ' objects2[2]
+
return {{{objects|objects}}}[1] ..' & ' {{{objects2|objects2}}}[2]
 
end
 
end
   
 
return p
 
return p
  +
|lang="lua"
</syntaxhighlight>
 
  +
}}
<pre>{{#invoke:showobjects|show}}
 
  +
{{#tag:pre|
Output: "food & garden"
 
  +
{<nowiki/>{#invoke:{{{showobjects|showobjects}}}<nowiki>|</nowiki>{{{show|show}}}}<nowiki/>}
</pre>
 
  +
}}
  +
;{{{output|Output}}}
  +
{{#tag:pre|
 
{{{fng|food & garden}}}
  +
}}
   
=== Global modules ===
+
=== {{{global-header|Global modules}}} ===
  +
{{Main|Global Lua Modules|{{l10n|Infobox Lua templating|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:
+
{{{global-instructions|Modules stored in <code>dev.fandom.com</code> 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. <code>food.fandom.com</code>). The difference lies only in the syntax (it uses "<code>Dev</code>" instead of "<code>Module</code>") used to obtain the modules:}}}
   
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
Line 207: Line 247:
 
</syntaxhighlight>
 
</syntaxhighlight>
   
== Tools ==
+
== {{{tools-header|Tools}}} ==
There are a bunch of useful tools that can help create modules:
+
{{{tools-instructions|There are a bunch of useful tools that can help create modules:}}}
   
=== Code-editor ===
+
=== {{{editor-header|Code-editor}}} ===
The code-editor (or ace-editor) - this is the default editor that can be disabled as needed.
+
{{{editor-instructions|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>.
+
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 ====
+
==== {{{keyboard-header|Keyboard shortcuts}}} ====
A brief list of some useful shortcuts is shown below:
+
{{{keyboard-instructions|A brief list of some useful shortcuts is shown below:}}}
  +
{| class="wikitable"
{|
 
 
!Windows/Linux
 
!Windows/Linux
 
!Mac
 
!Mac
!Action
+
!{{{action|Action}}}
 
|-
 
|-
|Alt-Shift-Down
+
|{{Keys|Alt+Shift+Down}}
|Command-Option-Down
+
|{{Keys|Command+Option+Down}}
|Copy lines down
+
|{{{copy-down|Copy lines down}}}
 
|-
 
|-
|Alt-Shift-Up
+
|{{Keys|Alt+Shift+Up}}
|Command-Option-Up
+
|{{Keys|Command+Option+Up}}
|Copy lines up
+
|{{{copy-up|Copy lines up}}}
 
|-
 
|-
|Alt-Down
+
|{{Keys|Alt+Down}}
|Option-Down
+
|{{Keys|Option+Down}}
|Move lines down
+
|{{{move-down|Move lines down}}}
 
|-
 
|-
|Alt-Up
+
|{{Keys|Alt+Up}}
|Option-Up
+
|{{Keys|Option+Up}}
|Move lines up
+
|{{{move-up|Move lines up}}}
 
|-
 
|-
|Alt-Delete
+
|{{Keys|Alt+Delete}}
|Ctrl-K
+
|{{Keys|Ctrl+K}}
|Remove to line end
+
|{{{remove-end|Remove to line end}}}
 
|-
 
|-
|Alt-Backspace
+
|{{Keys|Alt+Backspace}}
|Command-Backspace
+
|{{Keys|Command+Backspace}}
|Remove to linestart
+
|{{{remove-linestart|Remove to linestart}}}
 
|-
 
|-
|Ctrl-Backspace
+
|{{Keys|Ctrl+Backspace}}
|Option-Backspace, Ctrl-Option-Backspace
+
|{{Keys|Option+Backspace}}, {{Keys|Ctrl+Option+Backspace}}
|Remove word left
+
|{{{remove-left|Remove word left}}}
 
|-
 
|-
|Ctrl-Delete
+
|{{Keys|Ctrl+Delete}}
|Option-Delete
+
|{{Keys|Option+Delete}}
|Remove word right
+
|{{{remove-right|Remove word right}}}
 
|-
 
|-
 
|<nowiki>---</nowiki>
 
|<nowiki>---</nowiki>
|Ctrl-O
+
|{{Keys|Ctrl+O}}
|Split line
+
|{{{split|Split line}}}
 
|}
 
|}
  +
{| class="wikitable"
{|
 
 
!Windows/Linux
 
!Windows/Linux
 
!Mac
 
!Mac
!Action
+
!{{{action|Action}}}
 
|-
 
|-
|Ctrl-Shift-E
+
|{{Keys|Ctrl+Shift+E}}
|Command-Shift-E
+
|{{Keys|Command+Shift+E}}
|Macros replay
+
|{{{macros-replay|Macros replay}}}
 
|-
 
|-
|Ctrl-Alt-E
+
|{{Keys|Ctrl+Alt+E}}
 
|<nowiki>---</nowiki>
 
|<nowiki>---</nowiki>
|Macros recording
+
|{{{macros-recording|Macros recording}}}
 
|}
 
|}
   
=== Syntax highlighting and syntax checking ===
+
=== {{{syntax-check-header|Syntax highlighting and syntax checking}}} ===
The interactive code editor always highlights syntax errors, and sometimes gives helpful information to fix them.
+
{{{syntax-check-instructions|The interactive code editor always highlights syntax errors, and sometimes gives helpful information to fix them.}}}
   
=== Debug console ===
+
=== {{{debug-console-header|Debug console}}} ===
{{Main|Lua templating/Debug console}}
+
{{Main|Lua templating/Debug console|{{l10n|Infobox Lua templating|Debug-console}}}}
This is a console or terminal that makes it easy to test out code.
+
{{{debug-console-instructions|This is a console or terminal that makes it easy to test out code.}}}
   
  +
== {{i18n|getMsg|Documentation|see_also}} ==
== See also ==
 
* [[Help:Lua]]
+
* {{{help-lua|[[w:Help:Lua]]}}}
* [[Lua templating/Debug console]]
+
* [[Lua templating/Debug console|{{l10n|Infobox Lua templating|Debug-console}}]]
* [[Lua templating/Glossary]]
+
* [[Lua templating/Glossary|{{l10n|Infobox Lua templating|Glossary}}]]
* [[Lua templating/Style guide]]
+
* [[DEV:Lua]]
   
== References ==
+
== {{{references-header|References}}} ==
 
<references />
 
<references />
 
 
[[Category:Lua|Getting started]]
 
[[Category:Lua|Getting started]]
  +
</includeonly>

Latest revision as of 21:09, 13 July 2023

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

Basics

Main article: 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 7 seconds. This means that modules within a page cannot take longer than 7 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.fandom 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 code in the module above, if wrapped in a wikitext template, cannot access the parameters passed to the template. Such arguments can be accessed by first retrieving the parent frame using frame:getParent(), and then accessing 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 
Usage
{{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.

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

Main article: 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.[1]

Keyboard shortcuts

A brief list of some useful shortcuts is shown below:

Windows/Linux Mac Action
⎇ Alt + ⇧ Shift +  ⌘ Command + ⌥ Option +  Copy lines down
⎇ Alt + ⇧ Shift +  ⌘ Command + ⌥ Option +  Copy lines up
⎇ Alt +  ⌥ Option +  Move lines down
⎇ Alt +  ⌥ Option +  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: Debug console

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

See also

References

Text above can be found here (edit)