Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the explanation about backtick templates #5819

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -448,7 +448,7 @@
"title": "Equality",
"column": 0,
"category": "Language concepts",
"samples": [
"samples": [
{
"name": "Expression equality",
"url": "expression-equality",
Expand Down Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we need to handle redirection for this. cc @sm1990

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heshanpadmasiri please share the old url and the new url to update the google indexing

Copy link
Member Author

@heshanpadmasiri heshanpadmasiri Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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.
heshanpadmasiri marked this conversation as resolved.
Show resolved Hide resolved
heshanpadmasiri marked this conversation as resolved.
Show resolved Hide resolved
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);
MaryamZi marked this conversation as resolved.
Show resolved Hide resolved

// 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
Loading