Difference between revisions of "Module:Icon"
Jump to navigation
Jump to search
>Eave (initial example of how to make the wyrmprint icon logic in Lua) |
>Eave (initial example of how to make the wyrmprint icon logic in Lua) |
(No difference)
| |
Revision as of 19:29, 20 December 2019
Usage
This module is intended to back the rendering of more complicated Template:Icon cases. Currently it supports the following cases:
- Wyrmprints
In addition to main unnamed argument, it takes 3 additional optional named arguments:
- link: to specify a custom navigation link for the icon thumbnail
- size: to specify a custom size for the icon thumbnail
- text: (must be '1' for true or '0' for false) to specify if a text label should be appended after the icon. Defaults to false.
Example
Generally, this module should be called via the regular wikitext templates instead of directly. But here is example syntax for calling the functions from wikitext:
{{#invoke:Icon|Wyrmprint|Heralds of Hinomoto}}
Lua error: Error: Table Wyrmprints not found..
{{#invoke:Icon|Wyrmprint|Heralds of Hinomoto|size=60px}}
Lua error: Error: Table Wyrmprints not found..
{{#invoke:Icon|Wyrmprint|Heralds of Hinomoto|text=1}}
Lua error: Error: Table Wyrmprints not found..
{{#invoke:Icon|Wyrmprint|Heralds of Hinomoto|size=40px|link=Wyrmprints|text=1}}
Lua error: Error: Table Wyrmprints not found..
-- <includeonly> (prevents literal interpretation of links or categories within the module code)
-- Lua Templating for supporting Template:Icon.
local p = {}
local cargo = mw.ext.cargo
-- Usage: {{#invoke:Icon|Wyrmprint|<name>}}
-- Optional additional named args: 'link', 'size', 'text'
-- Ex: {{#invoke:Icon|Wyrmprint|Resounding Rendition}}
-- with custom link: {{#invoke:Icon|Wyrmprint|Resounding Rendition|link=Wyrmprints}}
-- with custom size: {{#invoke:Icon|Wyrmprint|Resounding Rendition|size=60px}}
-- with text label: {{#invoke:Icon|Wyrmprint|Resounding Rendition|text=1}}
function p.Wyrmprint(frame)
local name = mw.text.decode(frame.args[1], true)
local wyrmprint = cargoQuery{
tables = 'Wyrmprints',
fields = '_pageName,BaseId,Name',
args = {
where = '_pageName="'..name..'" OR Name="'..name..'"',
groupBy = '_pageName',
limit = 1
}
}[1] -- We only want the first result
if wyrmprint == nil then
return '[['..name..']]'
end
-- Format the content
local pageName = wyrmprint['_pageName']
local baseId = wyrmprint['BaseId']
local link = frame.args['link'] or pageName
local size = frame.args['size'] or '80px'
local text = frame.args['text'] == '1'
local thumbnail = '[[File:'..baseId..' 01.png|'..size..'|link='..link..']]'
if text then
return thumbnail..' [['..pageName..'|'..wyrmprint['Name']..']]'
else
return thumbnail
end
end
-- Helper functions
function cargoQuery(args)
return cargo.query(args.tables, args.fields, args.args)
end
return p
--</includeonly>