英文:
Passing an url to a template
问题
我试图将一个简单的mwurl传递给模板,但似乎并不总是起作用。
模板: U:
我的链接:{{{1}}}
调用代码:
{{U|[https://www.istaria-lexica.de/index.php?title=User:Elteria_Shadowhand/sandbox&action=edit 测试链接]}}
如果我使用google.com而不是链接,模板可以正常工作。我想这可能与特殊字符有关?我做错了什么?
英文:
I am trying to pass a simple mwurl to a template, but this doesn't seem to work always.
Template:U:
my link: {{{1}}}
Calling code:
{{U|[https://www.istaria-lexica.de/index.php?title=User:Elteria_Shadowhand/sandbox&action=edit test link]}}
If I use google.com instead, the template works. I assume there's some problem with special characters? What am I doing wrong?
答案1
得分: 1
问题出在URL中的等号。在模板参数语法中,等号具有特殊含义:等号左边的内容被视为参数的名称,等号右边的内容被视为参数的值。
你可以选择以下方式之一:
- 将URL作为命名参数传递,而不是匿名参数:
{{U|1=[https://www.istaria-lexica.de/index.php?title=User:Elteria_Shadowhand/sandbox&action=edit test link]}}, - 用预定义的(在MediaWiki的最新版本中定义的)模板
{{=}}替换所有等号:{{U|[https://www.istaria-lexica.de/index.php?title{{=}}User:Elteria_Shadowhand/sandbox&action{{=}}edit test link]}}。
此外:
- 也许,将方括号从模板参数移动到其定义中会是一个好主意:Template:U =
my link: [{{{1}}} {{{2}}}], - 如果链接指向同一维基,最好让维基引擎自动生成链接:
{{fullurl:User:Elteria Shadowhand/sandbox|action=edit}}或更好的是{{localurl:User:Elteria Shadowhand/sandbox|action=edit}}。模板定义中的样板代码也可以移动:Template:U =[{{localurl:{{{1}}}|action=edit}} {{{2}}}],调用方式为{{U|User:Elteria Shadowhand/sandbox|test link}}, - 如果链接位于要编辑的同一页面上,您可以使用
{{FULLPAGENAME}}:Template:U =[{{localurl:{{FULLPAGENAME}}|action=edit}} {{{1}}}],调用方式为{{U|test link}}, - 最后,如果链接文本始终相同,或者是链接页面名称,那么无需将其传递给模板:Template:U =
[{{localurl:{{FULLPAGENAME}}|action=edit}} test link],调用方式为{{U}},或者Template:U =[{{localurl:{{{1}}}|action=edit}} {{{1}}}],调用方式为{{U|User:Elteria Shadowhand/sandbox}}。
英文:
The problem is with the equal sign in the URL. It has a special meaning in the template parametre syntax: what is on the left of the equal sign is treated as the parametre's name, what is on the right, its value.
You can either:
- Pass the URL as a named parametre rather than an anonymous:
{{U|1=[https://www.istaria-lexica.de/index.php?title=User:Elteria_Shadowhand/sandbox&action=edit test link]}}, - Replace all equal signs with the pre-defined (in recent versions of MediaWiki) template
{{=}}:{{U|[https://www.istaria-lexica.de/index.php?title{{=}}User:Elteria_Shadowhand/sandbox&action{{=}}edit test link]}}.
In addition:
- perhaps, moving the square brackets from the template parameter to its definition would be a good idea: Template:U =
my link: [{{{1}}} {{{2}}}], - if the link is to the same wiki, it better to let the wiki engine form it:
{{fullurl:User:Elteria Shadowhand/sandbox|action=edit}}or, better,{{localurl:User:Elteria Shadowhand/sandbox|action=edit}}. The boilerplate code can be moved to the template definition as well: Template:U =[{{localurl:{{{1}}}|action=edit}} {{{2}}}], called with{{U|User:Elteria Shadowhand/sandbox|test link}}, - if the link is on the same page that is to be edited, you can use
{{FULLPAGENAME}}: Template:U =[{{localurl:{{FULLPAGENAME}}|action=edit}} {{{1}}}], called as{{U|test link}}, - finally, if the link text is always the same, or is the linked page name, there would be no need to pass it to the template: Template:U =
[{{localurl:{{FULLPAGENAME}}|action=edit}} test link], called as{{U}}, or Template:U =[{{localurl:{{FULLPAGENAME}}|action=edit}} {{FULLPAGENAME}}], also called as{{U}}, or Template:U =[{{localurl:{{{1}}}|action=edit}} {{{1}}}], called as{{U|User:Elteria Shadowhand/sandbox}}.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论