Swan Lake Beta3 is here.
import ballerina/io;

public function main() {
    string name = "James";

    // Concatenates `Hello, ` strings with the `name` value.
    string s1 = string`Hello, ${name}`;
    io:println(s1);

    // Concatenates `Backtick:` strings with `.
    string s2 = string`Backtick:${"`"}`;
    io:println(s2);
}

Backtick Templates

The backtick templates consist of a tag followed by characters surrounded by backticks. They can contain expressions in ${...} to be interpolated. If no escapes are recognized: use an expression to escape. They can contain newlines.

Processed in two phases.

  • Phase 1 does tag-independent parse: result is a list of strings and expressions
  • Phase 2 is tag-dependent


Phase 2 for string... converts expressions to strings and concatenates. base16 and base64 tags do not allow expressions.

import ballerina/io;
public function main() {
    string name = "James";
    string s1 = string`Hello, ${name}`;
    io:println(s1);

Concatenates Hello, strings with the name value.

    string s2 = string`Backtick:${"`"}`;
    io:println(s2);
}

Concatenates Backtick: strings with `.

bal run backtick_templates.bal
Hello, James
Backtick:`
import ballerina/io;

public function main() {
    string name = "James";

    // Concatenates `Hello, ` strings with the `name` value.
    string s1 = string`Hello, ${name}`;
    io:println(s1);

    // Concatenates `Backtick:` strings with `.
    string s2 = string`Backtick:${"`"}`;
    io:println(s2);
}

Backtick Templates

The backtick templates consist of a tag followed by characters surrounded by backticks. They can contain expressions in ${...} to be interpolated. If no escapes are recognized: use an expression to escape. They can contain newlines.

Processed in two phases.

  • Phase 1 does tag-independent parse: result is a list of strings and expressions
  • Phase 2 is tag-dependent


Phase 2 for string... converts expressions to strings and concatenates. base16 and base64 tags do not allow expressions.

import ballerina/io;
public function main() {
    string name = "James";
    string s1 = string`Hello, ${name}`;
    io:println(s1);

Concatenates Hello, strings with the name value.

    string s2 = string`Backtick:${"`"}`;
    io:println(s2);
}

Concatenates Backtick: strings with `.

bal run backtick_templates.bal
Hello, James
Backtick:`
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand