: 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>
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?
More posts by @BetL925
2 Comments
Sorted by latest first Latest Oldest Best
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;}}
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.