Code Include Demo.

3 min read

This post demonstrates how to include code files directly from the page bundle using the include-code shortcode.

Usage

To include a file from the post’s directory (page bundle), use the following syntax:

1
{{< include-code file="filename.ext" language="language-name" >}}
  • file: The name of the file relative to the post.
  • language (optional): The language for syntax highlighting. If omitted, it defaults to the file extension.

Python Example

Here is a Python script included from hello.py:

1
2
3
4
5
6
7
print("Hello from Python!")

def add(a, b):
    return a + b

result = add(10, 5)
print(f"Result: {result}")

Rust Example

Here is a Rust program included from main.rs:

1
2
3
4
5
fn main() {
    println!("Hello from Rust!");
    let x = 42;
    println!("The answer is {}", x);
}

JavaScript Example

Here is a JavaScript snippet included from script.js:

1
2
3
4
5
6
console.log("Hello from JavaScript!");

const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);

console.log(doubled);

Go Example

Here is a Go program included from main.go:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package main

import "fmt"

func main() {
    fmt.Println("Hello from Go!")
    
    messages := []string{"Hello", "World", "from", "Golang"}
    for _, msg := range messages {
        fmt.Println(msg)
    }
}

Java Example

Here is a Java class included from Main.java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello from Java!");
        
        int[] numbers = {1, 2, 3, 4, 5};
        for (int number : numbers) {
            System.out.println("Number: " + number);
        }
    }
}

Markdown Example

Here is a Markdown file included from example.md:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Hello from Markdown

This is a **bold** statement.
This is *italic*.

- Item 1
- Item 2
- Item 3

[Link to Google](https://google.com)

HTML Example

Here is an HTML file included from example.html:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Example</title>
</head>
<body>
    <h1>Hello from HTML!</h1>
    <p>This is a simple paragraph.</p>
    <button onclick="alert('Button clicked!')">Click Me</button>
</body>
</html>

Rendered Markdown Include

The following section is essentially “transcluded” from another file (included-section.md) using the include-md shortcode. Unlike the previous examples, this is rendered as actual content, not code.

This is an Included Markdown Section

This content is coming from a separate file named included-section.md.

  • It is rendered as Markdown.
  • Not displayed as a code block.

It effectively allows you to compose a single post from multiple source files.

Nested Code Example

Here’s a Python example included via include-code within this included Markdown section:

1
2
3
4
5
6
7
print("Hello from Python!")

def add(a, b):
    return a + b

result = add(10, 5)
print(f"Result: {result}")