Template:Code:Formatted
This is a template for writing code examples. You can specify the language the code is in, introductory text, code, output, and explanatory text. The code is syntax highlighted using the SyntaxHighlight extension, and the output is formatted with <pre>...</pre>
tags, meaning that it uses a monospace font and whitespace is preserved.
Usage
edit{{Code:Formatted | lang = | before = | code = | output = | after = | border-color = | background-color = | output-color = | width = }}
Parameters
editlang
- the language code. See the SyntaxHighlight extension page for a list of supported languages. The default language is Lua.before
- the introductory text. This comes before the code and the output.code
- the code.output
- the output.after
- the explanatory text. This comes after the code and the output.border-color
- the hexadecimal color code for the border color, e.g.FF7777
. Do not include a # symbol, as this is added by default.background-color
- the hexadecimal color code for the background color, e.g.FFDDDD
. Do not include a # symbol, as this is added by default.output-color
- the hexadecimal color code for the output text color, e.g.FF0000
. Do not include a # symbol, as this is added by default.width
- an optional width, e.g. "500px".
All parameters are optional.
Note: using a pipe character |
in any of the parameters will break the template. This is because for the MediaWiki software, the pipe character signifies the start of a new parameter. To work around this problem you can use the code {{!}}
.
Examples
editLua, all parameters
edit{{Code:Formatted | lang = lua | before = A hello world program. | code = print('Hello, world!') | output = Hello, world! | after = The <code>print</code> command prints text. }}
A hello world program.
print('Hello, world!')
Hello, world!
The
print
command prints text.Python, just code and output
edit{{Code:Formatted | lang = python | code = print("Hello, world!") | output = Hello, world! }}
PHP, Just code
edit{{Code:Formatted | lang = php | code = <?php echo "Hello, world!"; ?> }}
See also
edit- Template:Code, an overview of the different code templates.
- Template:Code:Lua, a version of this template that uses Lua syntax highlighting.