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?

2 Answers 2

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;}}
5
  • This seems to work.
    – qazwsx
    Commented Aug 17, 2014 at 6:58
  • Another problem is if the instance of code happens to contain triple {, i.e. {{{foobar}}}, it does not handle correctly.
    – qazwsx
    Commented Aug 18, 2014 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
    Commented Aug 18, 2014 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
    Commented Aug 21, 2014 at 17:32
  • 1
    @Problemania I don't think you can.
    – svick
    Commented Aug 22, 2014 at 0:18
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.

6
  • The problem is not what the template page shows, but how does it transclude and this doesn't fix that.
    – svick
    Commented Aug 15, 2014 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
    Commented Aug 15, 2014 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
    Commented Aug 15, 2014 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. Commented Aug 15, 2014 at 19:59
  • I copied your code verbatim to here and then transcluded it from here.
    – svick
    Commented Aug 15, 2014 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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