I want to create a MediaWiki template like

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

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;">

instead of

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

Any idea?


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;}}
  • This seems to work.
    – qazwsx
    Aug 17 '14 at 6:58
  • Another problem is if the instance of code happens to contain triple {, i.e. {{{foobar}}}, it does not handle correctly.
    – qazwsx
    Aug 18 '14 at 17:21
  • @Problemania That's normal for anything that's not considered code (and a parameter of a template is not considered code). To fix that, I think enclosing the code in <nowiki> should work, e.g. {{SourceCode|source=<nowiki>{{{foobar}}}</nowiki>}}.
    – svick
    Aug 18 '14 at 18:22
  • How to incorporate <nowiki> into the template? I find that using just {{#tag:syntaxhighlight|<nowiki>{{{code}}}</nowiki>|lang=java|style=background-color:lightgray; border:1px dashed gray;}} does not quite work.
    – qazwsx
    Aug 21 '14 at 17:32
  • 1
    @Problemania I don't think you can.
    – svick
    Aug 22 '14 at 0:18

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

<includeonly><syntaxhighlight lang="Java"></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.

  • The problem is not what the template page shows, but how does it transclude and this doesn't fix that.
    – svick
    Aug 15 '14 at 11:28
  • @svick I'm not sure I understand. This does fix how the template transcludes, doing what the OP requested; what the template page shows doesn't matter. Also, I tested your code, and it seems to have the same problem as using regular tags.
    – hsl
    Aug 15 '14 at 14:11
  • I tested your code and it didn't work fo me. And as the documentation you included says, <includeonly> only affects the template page, not how it's transcluded.
    – svick
    Aug 15 '14 at 19:42
  • @svick How are you testing it? It worked as expected for me. Perhaps you're confusing <includeonly> with <noinclude>, which doesn't affect the transclusion. Whatever is in <includeonly> tags is only included in a transclusion, not on the template page. Aug 15 '14 at 19:59
  • I copied your code verbatim to here and then transcluded it from here.
    – svick
    Aug 15 '14 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.