Template
Post
Template Expressions
Template Syntax uses two `
backtick letters to enclose a string with interpolated expressions.
✍ Syntax
Template Expression
[Tag] `
BacktickItem* Dollar* `
Here
- Tag =
string
|xml
|re
- BacktickItem := BacktickSafeChar| BacktickDollarsSafeChar| Dollar* interpolation
- interpolation :=
${
expression}
- BacktickSafeChar := ^ (
`
|$
) - BacktickDollarsSafeChar :=
$
+ ^ ({
|`
|$
) - Dollar :=
$
Template Kinds
Ballerina supports following template syntaxes:
Template Expressions | Example |
---|---|
String Template | string `Hello ${a}` |
XML Template | xml `<A>${a}</A>` |
Regular Expression Template | re `A.*?` |
Raw Data Template | `A ${a} B` |
👉 Template Expression uses interpolated expressions. Let’s say the expression you want to interpolate is expr
, then interpolated expression is written as ${expr}
. The semantics of the interpolated expression depend on each template kind. See each kind for more details.
👉 Byte Array Literal Syntax uses the same syntax as the Template expression, But it doesn’t support interpolated expressions and is always a constant expression. Because of these reasons, it is more like a literal, than a template expression.
Article
String Template
A String template expression constructs a string value using the Template expression.
January 1, 2023Article
XML Template
Use XML template expression to construct an XML value.
January 1, 2023Article
Raw Template Expression
Raw Template expression constructs an object belongs to object:RawTemplate
object type.