3

I want to create a MediaWiki template like

<syntaxhighlight lang="Java" style="background-color:lightgray; border:1px dashed gray;">
{{{code}}}
</syntaxhighlight>

and use it in normal article pages as

{{SourceCode|source=public static void main() {...}}}

but the problem is the <syntaxhighlight></syntaxhighlight> tags defeats {{{code}}} parameter, and the actual formatted result is

<syntaxhighlight lang="Java" style="background-color:lightgray; border:1px dashed gray;">
{{{code}}}
</syntaxhighlight>

instead of

<syntaxhighlight lang="Java" style="background-color:lightgray; border:1px dashed gray;">
public static void main() {...}
</syntaxhighlight>

Any idea?

qazwsx
  • 311
  • 1
  • 5

2 Answers2

2

The problem is that code inside “extension tags” like <syntaxhighlight> is not parsed, which is why the {{{code}}} is not replaced with the value of the code parameter.

What you need to use is to use {{#tag}} parser function:

{{#tag:syntaxhighlight|{{{code}}}|lang=java|style=background-color:lightgray; border:1px dashed gray;}}
svick
  • 282
  • 1
  • 7
0

Yes, you can do that with <includeonly> tags, like so: (custom styles removed for clarity)

<includeonly><syntaxhighlight lang="Java"></includeonly>
{{{code}}}
<includeonly></syntaxhighlight></includeonly>

From mediawiki.org:

The markup <includeonly>...</includeonly> means that the text between the tags will only be used when the page is transcluded onto another page, and will not appear on the page itself.

So basically the <syntaxhighlight> tags are ignored on the template page, fooling the parser into highlighting code parameter when it is transcluded on another page.

NinjaBearMonkey
  • 456
  • 3
  • 8