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

4

24

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.

Avar answered 3/1, 2014 at 5:49 Comment(1)
possible duplicate of Equals signs in Wikipedia template parameters won't display properlyAdamina
A
27

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.

Adamina answered 3/1, 2014 at 17:55 Comment(2)
Note that this won't work if more positional parameters follow, because specifying a positional parameter by name won't use up its positional slot. This template call: {{AnyTemplate|1=first arg|second arg}} will assign "second arg" to {{{1}}} and leave {{{2}}} undefined. In some cases, escaping '=' by providing Template:= and writing {{=}} (see answer below) may be the better way.Lyre
@straycat: That's a good point, although you can always do {{AnyTemplate|1=first arg|2=second arg}}.Adamina
C
14

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)) }} 
Covarrubias answered 3/1, 2014 at 5:54 Comment(2)
No love: "R Template:= 3/(2-(1+1))"Avar
Ah, then you should edit the [[Template:=]] page and save it with the single character =. I had thought that was a MediaWiki default but apparently it's specific to Wikipedia. I'll update my answer.Covarrubias
S
7

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 contents and then include it as {{=}}

Supernormal answered 3/8, 2017 at 10:12 Comment(3)
No, using = raw in Template:= will work just fine. The text created by expanding the templates in an template argument is never parsed again for additional arguments.Lyre
See also the source of Template:= on Wikipedia -- it uses a raw =; the rest is boilerplate for templates. They should know best ... :)Lyre
@Lyre thanks for the correction. I was erring on the side of caution, but you are right, it seems using a raw = is enoughSupernormal
L
1

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 }} }}
Lasko answered 12/11, 2015 at 2:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.