Skip to content

Commit

Permalink
Merge pull request #5819 from heshanpadmasiri/feat/backtick-template
Browse files Browse the repository at this point in the history
Improve the explanation about backtick templates
  • Loading branch information
MaryamZi authored Nov 19, 2024
2 parents 4643130 + 8c31c4c commit 3571e1d
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 45 deletions.
19 changes: 0 additions & 19 deletions examples/backtick-templates/backtick_templates.bal

This file was deleted.

12 changes: 0 additions & 12 deletions examples/backtick-templates/backtick_templates.md

This file was deleted.

2 changes: 0 additions & 2 deletions examples/backtick-templates/backtick_templates.metatags

This file was deleted.

4 changes: 0 additions & 4 deletions examples/backtick-templates/backtick_templates.out

This file was deleted.

12 changes: 5 additions & 7 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@
"title": "Equality",
"column": 0,
"category": "Language concepts",
"samples": [
"samples": [
{
"name": "Expression equality",
"url": "expression-equality",
Expand Down Expand Up @@ -1099,15 +1099,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
Expand Down
3 changes: 2 additions & 1 deletion examples/raw-templates/raw_templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
30 changes: 30 additions & 0 deletions examples/string-templates/string_templates.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import ballerina/io;

public function main() {
string word = "world";
string s0 = string `Hello, ${word}!`;
io:println(s0);

// Use interpolations to use escape characters.
// 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);

// A string template expression can be written on multiple lines by breaking at interpolations.
string s3 = string `T_1 is ${
1}, T_2 is ${1 +
2} and T_3 is ${1 + 2 + 3
}`;
io:println(s3);

// If there are no interpolations to break at a required point, you can introduce an interpolation with an empty
// string.
string s4 = string `prefix ${
""}middle ${""
}suffix`;
io:println(s4);
}
3 changes: 3 additions & 0 deletions examples/string-templates/string_templates.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions examples/string-templates/string_templates.metatags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This BBE demonstrates string templates in Ballerina.
keywords: ballerina, ballerina by example, bbe, string templates
7 changes: 7 additions & 0 deletions examples/string-templates/string_templates.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$ bal run string_templates.bal
Hello, world!
line one \n rest of line 1
second line
Backtick: ` Dollar: $
T_1 is 1, T_2 is 3 and T_3 is 6
prefix middle suffix

0 comments on commit 3571e1d

Please sign in to comment.