Codeblock Shortcode

The codeblock shortcode provides a fully styled code display with:

It is used to show clean code examples in your blog.


Usage

GO
1
2
3
{{< codeblock lang="javascript" >}}
console.log("Hello World");
{{< /codeblock >}}

Parameters

ParameterRequiredDefaultDescription
langYes-Programming language used for syntax highlighting
Inner contentYes-The code to be displayed inside the block
Table: Codeblock Parameters

Config

The Sans theme uses the monokai highlighter by default. You can change the code highlighter and other params in hugo.toml.

TOML
1
2
3
4
  [markup.highlight]
    style = "monokai"
    lineNos = true
    lineNumbersInTable = true

Features


Examples

JavaScript Example

JAVASCRIPT
1
2
3
4
5
6
{{< codeblock lang="javascript" >}}
function hello() {
  console.log("Hello World");
}
hello();
{{< /codeblock >}}

Python Example

PYTHON
1
2
3
4
5
6
{{< codeblock lang="python" >}}
def greet(name):
    return f"Hello, {name}"

print(greet("World"))
{{< /codeblock >}}

HTML Example

HTML
1
2
3
4
5
{{< codeblock lang="html" >}}
<div class="card">
  <p>Hello!</p>
</div>
{{< /codeblock >}}

Showing Another Shortcode Inside Codeblock

GO
1
2
3
4
5
{{< codeblock lang="go" >}}
{{< admonition type="info" >}}
This is inside an admonition block.
{{< /admonition >}}
{{< /codeblock >}}