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:`