diff --git a/examples/backtick-templates/backtick_templates.bal b/examples/backtick-templates/backtick_templates.bal deleted file mode 100644 index 2ab87ce3bc..0000000000 --- a/examples/backtick-templates/backtick_templates.bal +++ /dev/null @@ -1,19 +0,0 @@ -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); - - // If required, a single-line string template can be split into a multiline string template by breaking - // at an interpolation point or using string concatenation. - string s3 = string `A string-template-expr is evaluated by evaluating the expression in each interpolation in ${ - ""}the order in which they occur.`; - io:println(s3); -} diff --git a/examples/backtick-templates/backtick_templates.md b/examples/backtick-templates/backtick_templates.md deleted file mode 100644 index 511bdb502b..0000000000 --- a/examples/backtick-templates/backtick_templates.md +++ /dev/null @@ -1,12 +0,0 @@ -# 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 and are processed in two phases. - -1. Phase 1 does tag-independent parse: result is a list of `strings` and `expressions` -2. Phase 2 is tag-dependent: Phase 2 for `string...` converts `expressions` to `strings` and concatenates. `base16` and `base64` tags do not allow `expressions`. - -::: code backtick_templates.bal ::: - -::: out backtick_templates.out ::: \ No newline at end of file diff --git a/examples/backtick-templates/backtick_templates.metatags b/examples/backtick-templates/backtick_templates.metatags deleted file mode 100644 index a138d0cda9..0000000000 --- a/examples/backtick-templates/backtick_templates.metatags +++ /dev/null @@ -1,2 +0,0 @@ -description: This BBE demonstrates backtick templates in Ballerina. -keywords: ballerina, ballerina by example, bbe, backtick templates diff --git a/examples/backtick-templates/backtick_templates.out b/examples/backtick-templates/backtick_templates.out deleted file mode 100644 index 042cd85271..0000000000 --- a/examples/backtick-templates/backtick_templates.out +++ /dev/null @@ -1,4 +0,0 @@ -$ bal run backtick_templates.bal -Hello, James -Backtick:` -A string-template-expr is evaluated by evaluating the expression in each interpolation in the order in which they occur. diff --git a/examples/index.json b/examples/index.json index f0d6ec820f..c2eb3f3bb9 100644 --- a/examples/index.json +++ b/examples/index.json @@ -448,7 +448,7 @@ "title": "Equality", "column": 0, "category": "Language concepts", - "samples": [ + "samples": [ { "name": "Expression equality", "url": "expression-equality", @@ -1092,15 +1092,13 @@ { "name": "Raw templates", "url": "raw-templates", - "verifyBuild": false, - "verifyOutput": false, - "disableVerificationReason": "Includes ballerinax components", - "disablePlayground": true, + "verifyBuild": true, + "verifyOutput": true, "isLearnByExample": true }, { - "name": "Backtick templates", - "url": "backtick-templates", + "name": "String templates", + "url": "string-templates", "verifyBuild": true, "verifyOutput": true, "isLearnByExample": true diff --git a/examples/raw-templates/raw_templates.md b/examples/raw-templates/raw_templates.md index f40101316f..9c1fdbce63 100644 --- a/examples/raw-templates/raw_templates.md +++ b/examples/raw-templates/raw_templates.md @@ -11,6 +11,7 @@ An important use case of custom raw templates is SQL parameterized queries. ::: out raw_templates.out ::: ## Related links -- [Backtick templates](https://ballerina.io/learn/by-example/backtick-templates/) +- [String templates](https://ballerina.io/learn/by-example/string-templates/) +- [RegExp type](https://ballerina.io/learn/by-example/regexp-type/) - [Object type inclusion](https://ballerina.io/learn/by-example/object-type-inclusion/) - [Database Access - Simple query](https://ballerina.io/learn/by-example/mysql-query-operation/) diff --git a/examples/string-templates/string_templates.bal b/examples/string-templates/string_templates.bal new file mode 100644 index 0000000000..0f6550aada --- /dev/null +++ b/examples/string-templates/string_templates.bal @@ -0,0 +1,26 @@ +import ballerina/io; + +public function main() { + // Note how the first `\n` is treated as is. + string s1 = string `line one \n rest of line 1 ${"\n"} second line`; + io:println(s1); + + // Use interpolations to add the ` and $ characters. + string s2 = string `Backtick: ${"`"} Dollar: ${"$"}`; + io:println(s2); + + // You can use an empty string interpolation to break a template expression + // across multiple lines. + string s4 = string `prefix ${ + ""}middle ${"" + }suffix`; + io:println(s4); + + // In fact, the body of interpolations is treated as a normal expression. So you can add line breaks anywhere that + // is valid in an expression. + string s5 = string `T_1 is ${ + 1}, T_2 is ${1 + + 2} and T_3 is ${1 + 2 + 3 + }`; + io:println(s5); +} diff --git a/examples/string-templates/string_templates.md b/examples/string-templates/string_templates.md new file mode 100644 index 0000000000..de854c2bf5 --- /dev/null +++ b/examples/string-templates/string_templates.md @@ -0,0 +1,3 @@ +# String templates + +A string template is a template expression that can be used to create a string literal. It consists of the `string` tag and a sequence of characters interleaved with interpolations (in the form `${expression}`). Each interpolation must be a subtype of `boolean|int|float|decimal|string`. Every character not a part of the interpolation is interpreted as is to form a sequence of string literals broken at interpolations. This means if you want to add any escape characters you must add them as interpolations (for example `${"\n"}`). Every interpolation is converted to a string using the `toString` lang lib function and concatenated with the other string literals to form a single string literal. diff --git a/examples/string-templates/string_templates.metatags b/examples/string-templates/string_templates.metatags new file mode 100644 index 0000000000..2316bed77a --- /dev/null +++ b/examples/string-templates/string_templates.metatags @@ -0,0 +1,2 @@ +description: This BBE demonstrates string templates in Ballerina. +keywords: ballerina, ballerina by example, bbe, string templates diff --git a/examples/string-templates/string_templates.out b/examples/string-templates/string_templates.out new file mode 100644 index 0000000000..fef4ca8853 --- /dev/null +++ b/examples/string-templates/string_templates.out @@ -0,0 +1,6 @@ +$ bal run string_templates.bal +line one \n rest of line 1 + second line +Backtick: ` Dollar: $ +prefix middle suffix +T_1 is 1, T_2 is 3 and T_3 is 6