The Code Block Macro allows you to display source code in your document with the appropriate syntax highlighting. The code block displays on the page as shown below.
public static void main(String[] args) { System.out.println("Hello World!"); }
On this page:
Usage with the Macro Browser
To insert the code block macro into a page using the Macro Browser,
- Open the Confluence page or blog post that you want to edit, then click the 'Edit' button.
- Click the Macro Browser icon on the toolbar. The macro browser window will open.
- Scroll through the list of macros to find the one you want. Alternatively, start typing the macro name into the search box at the top right of the macro browser. Macros with a matching name will appear in the main pane.
- Click the desired macro.
- Set the macro parameters to your requirements. If desired, you can preview these changes by clicking 'Refresh'.
- Click 'Insert' to add the macro onto the page.
You can also insert macros via autocomplete. For more information, see Using Autocomplete in the Rich Text Editor.
Once you've found the code block macro, click 'insert' to add it to your page.
Usage with the Wiki Markup Editor
What you need to type |
What you will get |
---|---|
{code} public static void main(String[] args) {code} |
public static void main(String[] args) |
Parameters
Parameters are options that you can include in Confluence macros to control the content or format of the macro output. The table below lists relevant parameters for this macro.
Parameter names are different in the macro browser and in wiki markup. Below we show the macro browser parameter names in bold text, and the equivalent wiki markup parameters in (bracketed
) text. If we do not show any parameter name for the wiki markup, then you should leave out the parameter name and simply include the parameter value as the first parameter, immediately after the colon (:
).
Parameter |
Required? |
Default |
Description |
---|---|---|---|
Syntax highlighting |
no |
java |
Specifies the language (or environment) for syntax highlighting. The default language is Java but you can also choose from one of the following other languages/environments:
|
Title |
no |
none |
Adds a title to the code macro. If specified, the title will be displayed in its own row at the top of your code macro box. |
Collapsible |
no |
false |
If true, the code macro's content will be collapsed upon visiting or refreshing the Confluence page. Clicking the 'expand source' link allows you to view this content. |
Show line numbers |
no |
false |
If true, a line number will be shown to the left of each line of code, where each number is incremented by 1. |
First line number |
no |
1 |
When Show line numbers ( |
Theme |
no |
Default |
Specifies the colour scheme used for displaying your code. Many of these themes are based on the default colour schemes of popular Integrated Development Environments (IDEs). The default theme is Confluence (also known as Default), which is typically black and coloured text on a blank background. However, you can also choose from one of the following other popular themes:
|
Be aware that any white space contained between the {code} commands is not manipulated in any way by the Code Block Macro. This is to provide the writer with flexibility over code indentation.
All the optional parameters of the {panel} macro are valid for the {code} macro as well.
Examples
Example 1: Java
What you need to type |
What you will get |
---|---|
{code} |
public String getFoo() { return foo; } |
Example 2: XML
What you need to type |
What you will get |
---|---|
{code:XML} |
<test> <another tag="attribute"/> </test> |
RELATED TOPICS
Take me back to the Wikis Help Guide.