Return to homepage

DOSATO

Copyright © 2024 Sebastiaan Heins

1.11 Template Strings

Strings defined with backticks are called template strings.
They can be used to create strings with variables inside of them.
The syntax for a template string is `string {external_value}`.
Example of a template string:

make int a = 1
make int b = 2
make string result = `The result of {a} + {b} is {a + b}`

do say (result) // The result of 1 + 2 is 3

In the example above, the variables a and b are used inside of the template string.
The variables are enclosed in curly braces ${} and are replaced with their values.
The result of the expression a + b is also used inside of the template string.
The template string is then assigned to the variable result and printed to the console.
Template strings can also be used to create multi-line strings.
Example of a multi-line template string:

make string multiLine = `This is a
multi-line
string`

In the example above, the template string is split over multiple lines.
The newlines are preserved in the string.
The template string is assigned to the variable multiLine.