Template:Code: Difference between revisions

From BathAfrica Documentation
Jump to navigation Jump to search
m 2 revisions imported: Full 75-template migration with source revision history from wiki.sybma.com
m 1 revision imported: Make tested BathAfrica compatibility revisions current
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{#tag:syntaxhighlight
{{#tag:pre|{{{code|{{{1|}}}}}}}}<noinclude>
  |{{{code|{{{1}}}}}}
  |lang="{{{lang|{{{2|python}}}}}}"
}}<noinclude>


----
----
Line 10: Line 7:
This template should not be used to contain verbatim macro code in the [[Macros_recipes|Macros recipes]] pages. Macro code must be plain, free of any syntax highlight. For this, use [[:Template:MacroCode]].
This template should not be used to contain verbatim macro code in the [[Macros_recipes|Macros recipes]] pages. Macro code must be plain, free of any syntax highlight. For this, use [[:Template:MacroCode]].


This template essentially creates a block of text surrounded by wiki <code>&lt;sintaxhighlight&gt; ... &lt;/sintaxhighlight&gt;</code> tags, which are converted into appropriate <code>&lt;span&gt; ... &lt;/span&gt;</code> tags with the right color. See the full documentation in [https://www.mediawiki.org/wiki/Extension:SyntaxHighlight Extension:SyntaxHighlight].
Legacy documentation (syntax colouring is unavailable here; this migration uses a plain preformatted block). This template originally created a block of text surrounded by wiki <code>&lt;sintaxhighlight&gt; ... &lt;/sintaxhighlight&gt;</code> tags, which are converted into appropriate <code>&lt;span&gt; ... &lt;/span&gt;</code> tags with the right color. See the full documentation in [https://www.mediawiki.org/wiki/Extension:SyntaxHighlight Extension:SyntaxHighlight].


----
----
Line 130: Line 127:
     Q = Qt.Ok | Qt.Cancel
     Q = Qt.Ok | Qt.Cancel
</pre>
</pre>
<noinclude>
----
Migrated from [https://wiki.sybma.com/index.php?title=Template:Code SiteMasterDoc]. Original revision history and imported contributor names are retained. See [[BathAfrica:Template migration]] for compatibility changes and unresolved image dependencies. Legacy examples do not establish current BathAfrica product capabilities.
</noinclude>

Latest revision as of 07:18, 29 October 2026



Use this template when you want to display a block of code.

This template should not be used to contain verbatim macro code in the Macros recipes pages. Macro code must be plain, free of any syntax highlight. For this, use Template:MacroCode.

Legacy documentation (syntax colouring is unavailable here; this migration uses a plain preformatted block). This template originally created a block of text surrounded by wiki <sintaxhighlight> ... </sintaxhighlight> tags, which are converted into appropriate <span> ... </span> tags with the right color. See the full documentation in Extension:SyntaxHighlight.


Since content that includes an equals sign (=) is interpreted as a variable, you must specify the code= parameter explicitly to avoid issues.

The parameter lang= controls the coloration, depending on the programming language. The default option is lang=python.

  • lang=cpp
  • lang=xml
  • lang=java
  • lang=bash

Many more options are be supported.


Example 1:

{{Code|code=
import LabRPS
import Part
Line = Part.makeLine(LabRPS.Vector(0,0,0), LabRPS.Vector(2,0,0))
}}

Result:

import LabRPS
import Part
Line = Part.makeLine(LabRPS.Vector(0,0,0), LabRPS.Vector(2,0,0))


Example 2:

{{Code|lang=cpp|code=
DEF_STD_CMD_A(StdCmdMyCommand);

StdCmdMyCommand::StdCmdMyCommand()
  : Command("Std_My_Command")
{
    sMenuText     = QT_TR_NOOP("My Command");
    sToolTipText  = QT_TR_NOOP("Runs my command in the active document");
    sWhatsThis    = "Std_MyCommand";
    sPixmap       = "MyCommand.svg";
}
}}

Result:

DEF_STD_CMD_A(StdCmdMyCommand);

StdCmdMyCommand::StdCmdMyCommand()
  : Command("Std_My_Command")
{
    sMenuText     = QT_TR_NOOP("My Command");
    sToolTipText  = QT_TR_NOOP("Runs my command in the active document");
    sWhatsThis    = "Std_MyCommand";
    sPixmap       = "MyCommand.svg";
}



Inside the code, neither the double nor the triple braces {{ }}, nor the vertical bar | are allowed because these are interpreted in special ways by the wiki software.

{{Code|code=
a = {{"A"}, "B"}
if a:
    Q = Qt.Ok | Qt.Cancel
}}

Using the Equals sign '=' within this template will result in a broken output.



The <nowiki> ... </nowiki> tags can be added explicitly to allow displaying these special symbols.

{{Code|code=
<nowiki>
a = {{"A"}, "B"}
if a:
    Q = Qt.Ok | Qt.Cancel
</nowiki>
}}

Result:

{{Code|code=

a = {{"A"}, "B"} if a:

   Q = Qt.Ok | Qt.Cancel

}}


We can also display all symbols by using explicit <pre> ... </pre> tags. In this case, no syntax highlighting will exist, but the code will be displayed with all symbols intact.

<pre>
a = {{"A"}, "B"}
if a:
    Q = Qt.Ok | Qt.Cancel
</pre>

Result:

a = {{"A"}, "B"}
if a:
    Q = Qt.Ok | Qt.Cancel

Migrated from SiteMasterDoc. Original revision history and imported contributor names are retained. See BathAfrica:Template migration for compatibility changes and unresolved image dependencies. Legacy examples do not establish current BathAfrica product capabilities.