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:,  , and. 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, 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  code itself, and.

The  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. The  code is where things get interesting.

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

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  to control input we are given.) The output in Lua functions is provided by the   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  and before the parentheses. In other words, the function name is.

So why the ? Why not just make the name ? The answer is that the  is how we tell Scribunto that we want our function to be available from #invoke. If we leave out the, 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.)