Module talk:Template invocation: Difference between revisions

Content deleted Content added
Editor-hostile wikitext: fix proposal for a minor glitch
 
(8 intermediate revisions by 4 users not shown)
Line 56:
 
I propose to fix it by not adding a space for the first named parameter. A hacky implementation via variable <code>maybeSpace</code> in sandbox: [[Special:Diff/1205562670/1234263149]]. Tested at [[Special:Diff/1234262869]]. —⁠[[User:Andrybak|andrybak]] ([[User talk:Andrybak|talk]]) 12:44, 13 July 2024 (UTC)
 
:Courtesy pings: [[User:Pppery|Pppery]], [[User:Mikhail Ryazanov|Mikhail Ryazanov]]. —⁠[[User:Andrybak|andrybak]] ([[User talk:Andrybak|talk]]) 12:47, 13 July 2024 (UTC)
:: Fine with me. [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 15:46, 13 July 2024 (UTC)
:: {{Hidden ping|andrybak}}It's a tolerable workaround, but in principle, templates that use positional parameters must use {{tlx|trim}} in cases where leading/trailing spaces don't have any functional purpose (besides readability) but can break the result. — [[User:Mikhail Ryazanov|Mikhail Ryazanov]] ([[User talk:Mikhail Ryazanov|talk]]) 20:23, 13 July 2024 (UTC)
 
== Title clash in a few edge cases ==
 
{{editprotected|answered=yes}}
 
Currently, the "name" function returns the wrong result in three edge cases. Two of these are relatively straightforward to fix, but the third is more tricky:
# A page name like "Template:User:Theknightwho" should not return "User:Theknightwho", but instead should remain "Template:User:Theknightwho".
# The function should throw an error if it receives a title with an interwiki prefix (e.g. {{fr:foo}}) or one which starts with "#" (e.g. {{#foo}}), since both of these generate valid title objects, but neither can be used with template invocations.
# (Harder to fix): when a template name overlaps with that of a magic word, the "Template:" prefix is required in order to disambiguate it: e.g. <nowiki>{{Template:!}}</nowiki> or <nowiki>{{Template:PAGENAME:foo}}</nowiki>.
I've done a full implementation at [[wikt:Module:template parser#L-218]], but it relies on loading data for point 3, which might not be desirable to import to Wikipedia. However, points 1 and 2 are pretty simple to deal with. I've drafted a new version of the function below, incorporating fixes for both of them:
<syntaxhighlight lang=lua>
function p.name(title)
if type(title) == 'string' then
title = mw.title.new(title)
if not title or #title.prefixedText == 0 or #title.interwiki > 0 then
error("invalid title in parameter #1 of function 'name'", 2)
end
elseif type(title) ~= 'table' or type(title.getContent) ~= 'function' then
error("parameter #1 of function 'name' must be a string or a mw.title object", 2)
end
if title.namespace == 10 then
local text = title.text
local check = mw.title.new(text, 10)
-- Exclude the prefix, unless we have something like "Template:Category:Foo", which can't be abbreviated to "Category:Foo".
return check and mw.title.equals(title, check) and text or title.prefixedText
elseif title.namespace == 0 then
return ':' .. title.prefixedText
else
return title.prefixedText
end
end
</syntaxhighlight>
[[User:Theknightwho|Theknightwho]] ([[User talk:Theknightwho|talk]]) 20:07, 31 August 2024 (UTC)
: {{done}} and you should consider [[WP:Requests for permissions/Template editor|running for template editor here]] since you seem to know what you are doing. [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 20:22, 5 September 2024 (UTC)