Mobile app version of vmapp.org
Login or Join
BetL925

: Create MediaWiki template to format source code I want to create a MediaWiki template like <syntaxhighlight lang="Java" style="background-color:lightgray; border:1px dashed gray;"> {{{code}}} </syntaxhighlight>

@BetL925

Posted in: #Code #Configuration #Mediawiki

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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @BetL925

2 Comments

Sorted by latest first Latest Oldest Best

 

@Angela700

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

10% popularity Vote Up Vote Down


 

@Rivera981

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme