# Code blocks with syntax highlighting¶

## Quick Reference¶

• To insert a snippet as code with syntax highlighting, use the code-block directive or the shorthand ::.
• You can explicitly set the language in the code-block, you cannot in the shorthand.
• If you do not explicitly set the language, the default language (as set with the Highlight directive) is used. If no highlight directive was used, the default set in /Includes.rst.txt is used.
• It is recommended to use the short form (::) instead of code-block explicitly.
• Always use syntactically correct code in a code block.
• Use placeholders in angle brackets (<placeholder-name>) to refer to a place in the code where you don’t care about the exact value.

The following examples all do the same thing:

1. Use the shorthand :: (recommended):

See following example::

$a = 'b';  How this looks: See following example: $a = 'b';


You can use this, if the default language is already set to PHP with the highlight directive in the current file (or in Includes.rst.txt).

2. Set the language (PHP) in the code-block:

See following example:

.. code-block:: php

$a = 'b';  3. Use code-block without setting the language: See following example: .. code-block::$a = 'b';


You can use this, if you already set the language PHP with the highlight directive in the current file (or in Includes.rst.txt).

## Code block directive¶

Use codeblock with language PHP:

.. code-block:: php

$a = 'b';  Use codeblock without specifying language: .. code-block::$a = 'b';


This uses whatever language has last been set with the Highlight directive in the current file or in Includes.rst.txt.

## Use syntactically correct code¶

Attention

Syntax highlighting only works if the lexer can parse the code without errors. In other words: If there’s a syntax error in the code the highlighting will not work.

Wrong:

.. code-block:: php

$a = array( 'one' => 1, ... );  Correct: .. code-block:: php$a = array(
'one' => 1,
// ...
);


Sphinx uses Pygments for highlighting. On a machine that has Pygments installed the command pygmentize -L will list all available lexers.

## Highlight directive¶

You can set the default language with the highlight directive. All following code blocks will use the language as specified in the highlight directive for syntax highlighting.

If all of your code blocks in one file have the same language, it is easier to just set this once at the beginning of the file.

This way, you don’t need to set the language for each code-block (.. code-block:: LANG) explicitly and can use the shorthand notation.

Use reStructuredText highlighting:

.. highlight:: rst


Use PHP highlighting:

.. highlight:: php


For TYPO3 we have adopted the convention that each reStructuredText source file imports the Documentation/Includes.rst.txt file at the top. And in the included file - in general - we set PHP as default language for highlighting. Exception: In the TypoScript manuals we are using typoscript as default.

You can use the ..highlight:: LANG directive as often as you want. Each one remains valid up to the next or up to the end of the single file it is used in.

### Highlight language ‘guess’¶

Note that there is a - pseudo - language ‘guess’ as well. This should use the highlighter for the first language that Pygments finds to have no syntax error.

### Highlight language ‘none’¶

The pseudo language ‘none’ is recognized as well. In this case no highlighting will occur.

## Some more examples¶

### Add line numbers to code snippet¶

#### Source¶

.. code-block:: php
:linenos:

$GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields'] .= ',tx_realurl_pathsegment'; // Adjust to your needs$domain = 'example.org';
$rootPageUid = 123;$rssFeedPageType = 9818; // pageType of your RSS feed page


#### Result¶

 1 2 3 4 5 6 7 8 

### Turn off highlighting: Method 1¶

#### Source:¶

A description:

.. code-block:: none

$tree vendor/composer ├── ClassLoader.php ├── LICENSE ├── autoload_classmap.php ├── ... └── installed.json  #### Result¶ A description: $ tree vendor/composer
├── ...
└── installed.json


### Turn off highlighting: Method 2¶

#### Source:¶

.. highlight:: none

A description::

$tree vendor/composer ├── ClassLoader.php ├── LICENSE ├── autoload_classmap.php ├── ... └── installed.json  #### Result¶ A description: $ tree vendor/composer
├── ...
└── installed.json


## Available lexers¶

You can use any of the following names of lexers:

| bash, sh, ksh, shell | for example all mean the same lexer:

Tip: Try the Pygments Demo at http://pygments.org/

## Literalinclude¶

There also is a literalinclude directive.

## Placeholders¶

Placeholders in this context are named tags in code and example URLs where the exact value does not matter, but is referenced in the surrounding documentation. Use the Backus-Naur form <placeholder-name> for placeholders in code and URLs, i.e. use angle brackets to encapsulate the placeholder name.

For example in PHP

Set up a controller class to handle user interaction with the entity data
model:

.. code-block:: php

class <Entity>Controller extends ActionController
{
..
}

where <Entity> corresponds to the entity data model class name.


or on the command line

Importing a TYPO3 dump file is as simple as running:

.. code-block:: bash

typo3/sysext/core/bin/typo3 impexp:import <file>

where <file> can be the absolute path on the server or the relative path
in the TYPO3 project folder.


or in an example URL

The TYPO3 backend normally appends the session token ID to the URL as
follows: :samp:https://example.org/typo3/main?token=<token-id>.


In the XML and HTML markup languages, which make extensive use of angle brackets, the comment tag <!-- placeholder-name --> is used to insert placeholders. A <placeholder-name>` looks like a regular element and would lead to confusion.