Scribunto: An Introduction/Using invoke

If you are used to the syntax of MediaWiki templates, then you might be wondering about the syntax of the #invoke statement we saw in the previous chapter. The #invoke statement looked like this:


As you can see, there are three parts: #invoke:, Hello, and hello_world. If we compare this to a template that does the same thing - outputs the text "Hello, world!" - we can see that there is only one part.


The shared part is Hello, and in both cases this corresponds to the page name. For #invoke, it corresponds to the page Module:Hello, and for the template it corresponds to the page Template:Hello. This leaves us with two extra parts to the #invoke statement: the #invoke: code itself, and hello_world.

The #invoke: code is quite straightforward; all it is there to do is to tell the MediaWiki software that it is dealing with a Lua module, and not with a template or any other kind of special text.[1] The hello_world code is where things get interesting.

You may have noticed that hello_world appears in the middle of Module:Hello:

p = {}

function p.hello_world()
	return "Hello, world!"

return p

It appears in a block of that starts with the keyword "function" and ends with the keyword "end". This is a function. Functions in Lua are a lot like MediaWiki templates. You give templates input, the template processes the input, and it gives you some output. Functions are just the same; you give them input, the function processes the input, and it returns some output.

In this case, we are not providing any input, as our function is very simple. (Later we will learn how to use the parentheses after hello_world to control input we are given.) The output in Lua functions is provided by the return keyword. In this case we are saying, "make a function with no inputs that outputs the text 'Hello, world!'" At the same time, we have given the function a name. While there are other ways to define a function in Lua, in this example the function name is everything after the code function and before the parentheses. In other words, the function name is p.hello_world.

So why the p.? Why not just make the name hello_world? The answer is that the p. is how we tell Scribunto that we want our function to be available from #invoke. If we leave out the p., then we cannot run our function directly from any wiki pages. (There are ways that the function can be run indirectly, but we will not cover those for now.)

Notes edit

  1. Text that has a special function in MediaWiki is known as a magic word, and technically an #invoke statement is a particular kind of magic word called a parser function. You can see more documentation about magic words and parser functions at