Hello. I am trying to learn Lua on an example. I am trying to create a Lua script that displays an image of an item. I have made 50+ templates (example: Template:Coin) that display the wanted image, but maintaining it might be too much work. I thought I could compile all those templates to just one template with an array and for loop. The prefered syntax on the page would be:
{{I|coin|px}}
Where the 'I' would be the template, 'coin' would be the variable that goes into the template and 'px' would be an optional variable defining the width of the image. Here is the page I am testing on.
So the template would be 'Template:I'. The code on the template would probably be similar to this:
<onlyinclude>{{#invoke:Item|item|px}}</onlyinclude>
The Lua code gives me a really hard time. This is what I got so far:
local p = {}
local px = 40 --width of the image
local i = 'ruby' --where the item call should be saved {{I|i}}
local n = 1 --number of the index in array
local itemCall = {'coin', 'ruby', 'beam', 'bomb'}
local itemName = {'Coin', 'Ruby', 'Wooden beam', 'Bomb'}
local itemLink = {'Coin', 'Ruby', 'Wooden beam', 'Bomb (Horoku)'}
function p.item(itemCall,itemName,itemLink)
for k,j in ipairs(itemCall) do
if i == itemCall[j] then
n = j
end
end
return '[[File:' + itemName[n] + '.png|' + px + '|link=Items#' + itemLink[n] + ']]'
end
return p
The return is probably completely wrong. Also I don't know the difference between 'ipairs' and 'pairs'. Does that 'p' have to be in the code at all? How do you transfer the variable you put into the table on a page to the template and then into the Lua code (this is the most important thing)? Does #itemCall not return the length of the array/table? What does the 'local' thing do (aren't all variables local)?
I have looked through Lua reference manual and some of the modules on this wiki, but I can't figure it out. When I have discover templates I was really amazed at their implications and sad that I haven't discovered them sooner. But when I read in your frequently asked questions that Lua can do everything templates can do and more, I decided to learn Lua instead. Lua really sounds like a better version of templates, I just need to learn it first.