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 ExpressionsExample
String Templatestring `Hello ${a}`
XML Templatexml `<A>${a}</A>`
Regular Expression Templatere `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
String Template

A String template expression constructs a string value using the Template expression.

January 1, 2023

 Article

XML Template
XML Template

Use XML template expression to construct an XML value.

January 1, 2023

 Article

Raw Template Expression
Raw Template Expression

Raw Template expression constructs an object belongs to object:RawTemplate object type.

January 1, 2023
Template Expressions
 January 1, 2023

Table of Content

Key Words


Navigation

Site Settings

Site Theme

Source Code Density