Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question

migrated from webapps.stackexchange.com Aug 15 at 15:55

This question came from our site for power users of web applications.

2 Answers 2

up vote 1 down vote accepted

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;}}
share|improve this answer
    
This seems to work. –  qazwsx Aug 17 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 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 at 18:22
    
How to incorporate <nowiki> into the template? I find that using just {{#tag:syntaxhighlight|<nowiki>{{{code}}}</nowiki>|lang=java|style=background-c‌​olor:lightgray; border:1px dashed gray;}} does not quite work. –  qazwsx Aug 21 at 17:32
1  
@Problemania I don't think you can. –  svick Aug 22 at 0:18

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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