Template:Code/doc
![]() | This is a documentation subpage for Template:Code. It may contain usage information, categories and other content that is not part of the original template page. |
![]() | Due to a server limit of 500 "expensive parser function" calls (e.g. #ifexist:, PAGESIZE:, and PAGESINCATEGORY:), this template will only work properly if the page using it has not already exceeded the limit. When the limit is exceeded, the page using this template or module is categorised in Category:Pages with too many expensive parser function calls. (further information) |
Usage
Wraps a short span of text in <syntaxhighlight>
tags (see mw:Extension:SyntaxHighlight). This template should be used for short samples; longer content should use <pre>...</pre>
or <syntaxhighlight>...</syntaxhighlight>
. See Help:Wiki markup for an explanation of what the various tags do.
If the content includes an equals sign (=), you must specify the parameter explicitly: {{code
The template uses the <syntaxhighlight>
tag with the attribute inline=1
. This works like the combination of the <code>
and <nowiki>
tags, applied to the expanded wikitext. For example, {{code
- <syntaxhighlight lang="text" class="" style="" inline="1">some wiki text</syntaxhighlight>
If the above example is declared as wikitext (via {{code
- <syntaxhighlight lang="wikitext" class="" style="" inline="1">some wiki text</syntaxhighlight>
However, {{code
- <syntaxhighlight lang="wikitext" class="" style="" inline="1">a {{[[Template:{{{1}}}|{{{1}}}]]}} call</syntaxhighlight>
Use <nowiki>...</nowiki>
around the template name to avoid this problem:
- <syntaxhighlight lang="wikitext" class="" style="" inline="1">a {{template}} call</syntaxhighlight>
When used inline with regular text, {{code}}
generally looks best and is easiest to read when it is explicitly spaced apart from the regular text:
- <syntaxhighlight lang="wikitext" class="" style="" inline="1">foo {{code|bar baz}} quux.</syntaxhighlight>
is well spaced:
- foo <syntaxhighlight lang="text" class="" style="" inline="1">bar baz</syntaxhighlight> quux.
versus:
- <syntaxhighlight lang="wikitext" class="" style="" inline="1">foo {{code|bar baz}} quux.</syntaxhighlight>
which is less visually clear:
- foo <syntaxhighlight lang="text" class="" style="" inline="1">bar baz</syntaxhighlight> quux.
because "foo" and "<syntaxhighlight lang="text" class="" style="" inline="1">bar</syntaxhighlight>" will seem more closely associated than "<syntaxhighlight lang="text" class="" style="" inline="1">bar</syntaxhighlight>" and "<syntaxhighlight lang="text" class="" style="" inline="1">baz</syntaxhighlight>"; the width of the space character in a monospaced font is almost always larger than in a proportional font.
Use parameter {{{2}}} (unnamed, as |2=
, or more explicitly as |lang=
) to specify a language for mw:Extension:SyntaxHighlight. This option defaults to |lang=text
, i.e. no highlighting. Valid values include |lang=wikitext
for wikitext, |lang=html
for HTML, along with many other languages including <syntaxhighlight lang="text" class="" style="" inline="1">php</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">perl</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">css</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">javascript</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">mysql</syntaxhighlight>. Attempting to use an invalid value for lang will cause the page to be added to Category:Pages with syntax highlighting errors, and no syntax highlighting will be present in the output. <syntaxhighlight lang="text" class="" style="" inline="1">html4strict</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">html5</syntaxhighlight> used to be valid values - however, |lang=html
should be used instead.
This template does not need to be substituted.
Examples
Markup | Renders as |
---|---|
Lorem {{code|ipsum '''dolor'''}} sit amet |
Lorem <syntaxhighlight lang="text" class="" style="" inline="1">ipsum dolor</syntaxhighlight> sit amet |
Lorem {{code|ipsum '''dolor'''|wikitext}} sit amet |
Lorem <syntaxhighlight lang="wikitext" class="" style="" inline="1">ipsum dolor</syntaxhighlight> sit amet |
The declaration {{code|lang=cpp |int foo(const std::string& bar, const std::vector<long double*>& baz);}} is the prototype for a function defined later. |
The declaration <syntaxhighlight lang="cpp" class="" style="" inline="1">int foo(const std::string& bar, const std::vector<long double*>& baz);</syntaxhighlight> is the prototype for a function defined later. |
If the code contains an [[equals sign]], such as {{code |lang=javascript |code=var img = document.getElementsByTagName("img");}}, you must identify the first parameter explicitly as {{{1}}} or {{{code}}}; see also [[:bugzilla:5138]]. |
If the code contains an equals sign, such as <syntaxhighlight lang="javascript" class="" style="" inline="1">var img = document.getElementsByTagName("img");</syntaxhighlight>, you must identify the first parameter explicitly as {{{1}}} or {{{code}}}; see also bugzilla:5138. |
Included templates
Embedded templates do not function as expected inside {{code}}; for longer, free-form blocks of code, which can contain templates such as {{var}} and {{samp}}, use <code>...</code>
as a wrapper instead of this template.
Templates used inside {{code}} expose the rendered HTML— this can be useful. For example:
Markup | Renders as |
---|---|
{{code| {{cite web |title=Title |url=http://example.org}} |lang=html }} |
<syntaxhighlight lang="html" class="" style="" inline="1"> "Title". </syntaxhighlight> |
The above example shows the HTML rendered by the citation template, including the hidden metadata.
Expensive Parser Function warning
This template internally uses mw:Extension:SyntaxHighlight, which is considered an 'expensive parser function' (see WP:EXPENSIVE). If used on a page which uses more than 500 expensive parser functions, the output of subsequent uses of this template will be presented using <code>...</code>
formatting (without any syntax highlighting) instead.
TemplateData
TemplateData for Code
Parameter | Description | Type | Status | |
---|---|---|---|---|
Code | 1 code | The code to display. | String | required |
Language | 2 lang | The programming language of the source code. List of valid values is at: mw:Extension:SyntaxHighlight#Supported_languages
| String | suggested |
Class | class | String | optional | |
CSS Style | style | String | optional |
See also
- {{PreCode}}, which forbids line breaks inside the code
- {{codett}}, wrapper for this template that removes background and border styling
- {{mono}}, monospaced font only
- {{param value}}, monospaced; friendly handling for blank space
- {{Syntaxhighlight}}
- {{kbd}}, for keyboard input
- Help:Wiki markup