Passing an equal sign ('=') to a parameter in a MediaWiki template

自作多情 提交于 2019-12-06 17:04:17

问题


How can I use a '=' character in a template parameter without breaking the template parser? I'm not a MediaWIKI developer so I haven't debugged the code or checked the logs, I'm hoping someone here has a tip for escaping characters passed to templates.

Create a template called "Test" with this content:

{{{1}}}

Like this:

{{ Test | R = 3/(2-(1+1)) }} 

Will render {{{1}}} instead of the complex formula! I've determined the '=' character is the culprit.


回答1:


If a MediaWiki template parameter string contains an equals sign, everything before the sign is taken to be the name of the parameter. If it does not contain an equals sign, the parameter string is assigned to the next available numeric parameter.

Thus, the simplest workaround, if you actually want a numbered parameter value to contain an equals sign, is to explicitly number it, like this:

{{ Test | 1 = R = 3/(2-(1+1)) }}

This will cause {{{1}}} inside the template to expand to the string R = 3/(2-(1+1)), just as:

{{ Test | equation = R = 3/(2-(1+1)) }}

will cause {{{equation}}} to expand to that same string.




回答2:


You can create a {{=}} template whose value is =. Then use that template in place of the bare equal sign in your templates, like so:

{{ Test | R {{=}} 3/(2-(1+1)) }} 



回答3:


I'm surprised no one has mentioned this, but what about escaping the character?

Using = will work. If you can't be bothered to remember the code, you can create the template Template:= with = as the only content (using a non escaped = in that template will just cause the same issue again) and then include it as {{=}}




回答4:


If Extension:Variables is enabled, use a variable. Variable definitions preserve almost all symbols, including = and |, they only perform template expansion. So it's a reasonably safe approach when dealing with complex expressions and URLs.

{{ #vardefine: myequation | R = 3/(2-(1+1)) }}
{{ Test | {{ #var: myequation }} }}


来源:https://stackoverflow.com/questions/20897400/passing-an-equal-sign-to-a-parameter-in-a-mediawiki-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!