Module:Iteration/doc: Difference between revisions
More actions
mNo edit summary |
Fix language-code links to use Wikipedia interwiki prefixes |
||
| (5 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
<includeonly>{{Module rating|release}}{{Module rating|protected}}{{Heavily used module|very|edit=sandbox}}</includeonly> | <includeonly>{{Module rating|release}}{{Module rating|protected}}{{Heavily used module|very|edit=sandbox}}</includeonly> | ||
{{#tag:span|{{0}} The module uses frequently the template {{ | {{#tag:span|{{0}} The module uses frequently the template {{tlg|code=on|=}} for '''title = "="''' {{0}}|style=border:4px solid #CCC;margin-left:5.2em;background-color:#DFD}} | ||
This module contains the functions for different templates, solving their '''iteration problem''' | This module contains the functions for different templates, solving their '''iteration problem''' | ||
The MediaWiki template coding does not support the repetitive processing of a parameter set, which leads often to a very primitive and long chain of parameter checks in the kind of:<syntaxhighlight lang="text"> | |||
{{#if:{{{1|}}} | perform an action with parameter 1 }} | {{#if:{{{1|}}} | perform an action with parameter 1 }} | ||
{{#if:{{{2|}}} | perform an action with parameter 2 }} | {{#if:{{{2|}}} | perform an action with parameter 2 }} | ||
| Line 11: | Line 11: | ||
••• | ••• | ||
••• | ••• | ||
{{#if:{{{33|}}} perform an action with parameter 33 }}</ | {{#if:{{{33|}}} perform an action with parameter 33 }} | ||
with the disadvantage that such a construct will fail as soon as there comes a | </syntaxhighlight>with the disadvantage that such a construct will fail as soon as there comes a {{Ordinal|34}} parm. | ||
I always thought that with Lua and its < | I always thought that with Lua and its <syntaxhighlight lang="lua" inline>for</syntaxhighlight> loops this iterations should be possible, somehow. After long searching without any success and without an idea how to perform it, I asked at several forums and got finally from {{Ut|Trappist the monk|+|w:en}} the helping hint to solve it with a control structure like:<syntaxhighlight lang="lua"> | ||
After long searching without any success and without an idea how to perform it, I asked at several forums | |||
and got finally from {{Ut|Trappist the monk|+|en}} the helping hint to solve it with a control structure like | |||
local out = {} | local out = {} | ||
for i, v in ipairs (args) do | for i, v in ipairs (args) do | ||
table.insert (out, frame:expandTemplate{ title = title, args = v }) | table.insert (out, frame:expandTemplate{ title = title, args = v }) | ||
end | end | ||
return table.concat (out) | return table.concat (out) | ||
The functions contained therein are from very simple, just one parameter to pass, | </syntaxhighlight> | ||
to more complicated ones, where e.g. a pair of parameters needs kind of a flip-flop switch | The functions contained therein are from very simple, just one parameter to pass, to more complicated ones, where e.g. a pair of parameters needs kind of a flip-flop switch when always a tuple of e.g. an item and its correlated text needs to be passed. | ||
when always a tuple of e.g. an item and its correlated text needs to be passed. | |||
When there are tuples, triples etc., the <syntaxhighlight lang="lua" inline>table.insert</syntaxhighlight> occurs, when the last element is processed; this performs problems when the last group is incomplete – it should be finished with empty values to trigger the <syntaxhighlight lang="lua" inline>table.insert</syntaxhighlight>. The check with <syntaxhighlight lang="lua" inline>table.maxn</syntaxhighlight> seems not to work correctly? | |||
When there are tuples, triples etc., the < | |||
this performs problems when the last group is incomplete – it should be finished with empty values | ''This'' problem is solved differently, for the function <syntaxhighlight lang="lua" inline>attribs</syntaxhighlight> with a primitive workaround. | ||
to trigger the < | |||
''This'' problem is solved differently, for the function "attribs | == Namespace lists == | ||
There are special lists for some namespaces: | |||
* {{T\|Filelist|08}} - for items of the [[#Filelist|namespace 6]] (File:) | |||
* {{T\|Userlist|08}} - for items of the [[#Userlist|namespace 2]] (User:) | |||
* {{T\|Linklist|08}} - for items of the namespace <s>0</s> (main) — ''not yet'' | |||
* {{T\|Templist|08}} - for items of the namespace <s>10</s> (Template:) — ''not yet'' | |||
* {{T\|Cat-list|08}} - for items of the namespace <s>14</s> (Category:) — ''not yet'' | |||
* {{T\|Mod-list|08}} - for items of the namespace <s>828</s> (Module:) — ''not yet'' | |||
=== {{T\|Filelist|08}} === | |||
==== File list templates ==== | |||
The three templates {{tlg|code=on|Ifim}}, {{tlg|code=on|Ifim1}} and {{tlg|code=on|Ifimt}} avoid to display the file itself as an {{para|other version}}. | |||
There are currently five templates creating file lists with the function:<syntaxhighlight lang="lua" inline>filelist</syntaxhighlight> | |||
# {{tlg|code=on|Other versions}} a vertical list (without initial) | |||
There are currently five templates creating file lists with the < | # {{tlg|code=on|Filelist}} a horizontal list (without initial) | ||
# {{ | # {{tlg|code=on|Derivative versions}} a vertical list | ||
# {{ | # {{tlg|code=on|Derived from}} a vertical list | ||
# {{ | # {{tlg|code=on|File}} a horizontal list | ||
# {{ | All these templates are {{C|Autoreferencing templates|autoreferencing}}, and the support also the {{para|gallery}} option. | ||
# {{ | |||
All these templates are {{C|Autoreferencing templates|autoreferencing}}, and the support also the | |||
=== Other file lists === | ==== Other file lists ==== | ||
The < | The function:<syntaxhighlight lang="lua" inline>svglang</syntaxhighlight> creates a list of just file_names and language_codes, for: | ||
#<li value="6"> {{ | #<li value="6"> {{tlg|code=on|Svg lang}} | ||
# {{ | # {{tlg|code=on|Lang gallery}}<!-- and ''using'' the <code>file</code> parameter: --> | ||
# {{ | # {{tlg|code=on|Multilingual SVG diagrams}} | ||
Much more is created by the < | Much more is created by the function:<syntaxhighlight lang="lua" inline>ownbased</syntaxhighlight> for the template: | ||
#<li value="9">{{ | #<li value="9">{{tlg|code=on|Own based}} | ||
which will handle in the ''majority of cases'' just one file, but can serve an unlimited number. | which will handle in the ''majority of cases'' just one file, but can serve an unlimited number. | ||
This list can be controlled by the user to be displayed either horizontally or vertically. | |||
#<li value="10">{{ | This list can be controlled by the user to be displayed either horizontally or vertically. | ||
#<li value="10">{{tlg|code=on|Attribs}} | |||
The template works, whether there is a user assigned for a file, or one filename follows another. | The template works, whether there is a user assigned for a file, or one filename follows another. | ||
#<li value="11">{{tlg|code=on|Emoji}} | |||
The template displays a line of SVG emojis; Unicode characters/emojis can be displayed with the function <syntaxhighlight lang="lua" inline>emodis</syntaxhighlight> or <syntaxhighlight lang="lua" inline>emotab</syntaxhighlight>. | |||
==== Variable iterations ==== | |||
A very simple iteration, usable for everything, is performed by function:<syntaxhighlight lang="lua" inline>iterate</syntaxhighlight> | |||
:used by e.g. {{tlg|code=on|Image extracted}}, {{tlg|code=on|Image translated}}, | |||
More usable is the function:<syntaxhighlight lang="lua" inline>iteration</syntaxhighlight> for transclusions of a template with: | |||
*a number of positional parameters which can change at each transclusion, and | |||
*a number of positional parameters which are the same for all transclusions. | |||
See template {{tlg|code=on|Iterate}}. | |||
The function:<syntaxhighlight lang="lua" inline>loop</syntaxhighlight> is used by {{tlg|code=on|Repeat}}. | |||
=== | ==== <syntaxhighlight lang="lua" inline>samefile</syntaxhighlight> function ==== | ||
When the ''name'' of one of the files is the ''same'' as the first one, it is enough to reference it with its ''extension'' after a dot, e.g. just ".jpeg". The ''most standard'' extensions can be abbreviated, e.g. ".j" will be sufficient for ".jpg" (only for lower case!). | |||
The function <syntaxhighlight lang="lua" inline>incrhx</syntaxhighlight> increments a hexadecimal number (parm 1) by a decimal number (parm 2). | |||
Valid numbers are assumed, no checking occurs! | |||
When parameter 2 is preceded by a minus, the first number is ''de''cremented by the second one. | |||
=== {{T\|Userlist|08}} === | |||
* {{T\|Userlist|08}}: {{tlg|code=on|Userlist}} with many formatting parameters | |||
=== | === {{T\|Linklist|08}} === | ||
=== | === {{T\|Templist|08}} === | ||
== Other iterations == | |||
The function " | * {{T\|Langlist|08}}: | ||
* {{T\|Colorbox|08}}: {{tlg|code=on|ColorString}} and other color box templates | |||
* {{T\|Legends |08}}: {{tlg|code=on|Legtab}} combines single {{tlg|code=on|Legend}}s | |||
* {{T\|Tleparms|08}}: the metatemplate {{tlg|code=on|Tle}} displays the parameter usage of a template | |||
* {{T\|Navigate|08}}: {{tlg|code=on|Navcat}} navigates through color categories | |||
* {{T\|Showname|08}}: {{tlg|code=on|Showname}} helper template, for parameter tests | |||
* {{T\|Emodis {{0}}|08}}: {{tlg|code=on|Emoji}} displays (one or many) Unicode characters | |||
:The function <syntaxhighlight lang="lua" inline>emodis</syntaxhighlight> <u>dis</u>plays Unicode characters / <u>emo</u>jis; it can care for newlines, while | |||
:The function <syntaxhighlight lang="lua" inline>emotab</syntaxhighlight> displays a <u>tab</u>le of files / <u>emo</u>jis. | |||
Latest revision as of 18:44, 12 August 2026
The module uses frequently the template {{=}} for title = "="
This module contains the functions for different templates, solving their iteration problem
The MediaWiki template coding does not support the repetitive processing of a parameter set, which leads often to a very primitive and long chain of parameter checks in the kind of:
{{#if:{{{1|}}} | perform an action with parameter 1 }}
{{#if:{{{2|}}} | perform an action with parameter 2 }}
{{#if:{{{3|}}} | perform an action with parameter 3 }}
{{#if:{{{4|}}} | perform an action with parameter 4 }}
•••
•••
{{#if:{{{33|}}} perform an action with parameter 33 }}
with the disadvantage that such a construct will fail as soon as there comes a 34th parm.
I always thought that with Lua and its for loops this iterations should be possible, somehow. After long searching without any success and without an idea how to perform it, I asked at several forums and got finally from User:Trappist the monk the helping hint to solve it with a control structure like:
local out = {}
for i, v in ipairs (args) do
table.insert (out, frame:expandTemplate{ title = title, args = v })
end
return table.concat (out)
The functions contained therein are from very simple, just one parameter to pass, to more complicated ones, where e.g. a pair of parameters needs kind of a flip-flop switch when always a tuple of e.g. an item and its correlated text needs to be passed.
When there are tuples, triples etc., the table.insert occurs, when the last element is processed; this performs problems when the last group is incomplete – it should be finished with empty values to trigger the table.insert. The check with table.maxn seems not to work correctly?
This problem is solved differently, for the function attribs with a primitive workaround.
Namespace lists
editThere are special lists for some namespaces:
- Filelist - for items of the namespace 6 (File:)
- Userlist - for items of the namespace 2 (User:)
- Linklist - for items of the namespace
0(main) — not yet - Templist - for items of the namespace
10(Template:) — not yet - Cat-list - for items of the namespace
14(Category:) — not yet - Mod-list - for items of the namespace
828(Module:) — not yet
Filelist
editFile list templates
editThe three templates {{Ifim}}, {{Ifim1}} and {{Ifimt}} avoid to display the file itself as an |other version=.
There are currently five templates creating file lists with the function:filelist
{{Other versions}}a vertical list (without initial){{Filelist}}a horizontal list (without initial){{Derivative versions}}a vertical list{{Derived from}}a vertical list{{File}}a horizontal list
All these templates are autoreferencing, and the support also the |gallery= option.
Other file lists
editThe function:svglang creates a list of just file_names and language_codes, for:
-
{{Svg lang}} {{Lang gallery}}{{Multilingual SVG diagrams}}
Much more is created by the function:ownbased for the template:
{{Own based}}
which will handle in the majority of cases just one file, but can serve an unlimited number.
This list can be controlled by the user to be displayed either horizontally or vertically.
{{Attribs}}
The template works, whether there is a user assigned for a file, or one filename follows another.
{{Emoji}}
The template displays a line of SVG emojis; Unicode characters/emojis can be displayed with the function emodis or emotab.
Variable iterations
editA very simple iteration, usable for everything, is performed by function:iterate
- used by e.g.
{{Image extracted}},{{Image translated}},
More usable is the function:iteration for transclusions of a template with:
- a number of positional parameters which can change at each transclusion, and
- a number of positional parameters which are the same for all transclusions.
See template {{Iterate}}.
The function:loop is used by {{Repeat}}.
samefile function
edit
When the name of one of the files is the same as the first one, it is enough to reference it with its extension after a dot, e.g. just ".jpeg". The most standard extensions can be abbreviated, e.g. ".j" will be sufficient for ".jpg" (only for lower case!).
The function incrhx increments a hexadecimal number (parm 1) by a decimal number (parm 2).
Valid numbers are assumed, no checking occurs!
When parameter 2 is preceded by a minus, the first number is decremented by the second one.
Userlist
edit- Userlist:
{{Userlist}}with many formatting parameters
Linklist
editTemplist
editOther iterations
edit- Langlist:
- Colorbox:
{{ColorString}}and other color box templates - Legends :
{{Legtab}}combines single{{Legend}}s - Tleparms: the metatemplate
{{Tle}}displays the parameter usage of a template - Navigate:
{{Navcat}}navigates through color categories - Showname:
{{Showname}}helper template, for parameter tests - Emodis :
{{Emoji}}displays (one or many) Unicode characters
- The function
emodisdisplays Unicode characters / emojis; it can care for newlines, while - The function
emotabdisplays a table of files / emojis.