-
Notifications
You must be signed in to change notification settings - Fork 192
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
MaryamZi
merged 9 commits into
ballerina-platform:master
from
heshanpadmasiri:feat/backtick-template
Nov 19, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a9e5bb4
Move backtick templates to string templates
heshanpadmasiri 5bf42cb
Fix string templates
heshanpadmasiri 73ef2f3
Fix raw templates not running tests
heshanpadmasiri 61a3f56
Add regex example
heshanpadmasiri c61fad0
Remove unwanted changes
heshanpadmasiri dc37276
Apply suggestions from code review
heshanpadmasiri 0c7b30a
Remove regex_templates
heshanpadmasiri 714c721
Address review comments
heshanpadmasiri 8c31c4c
Update examples/string-templates/string_templates.bal
MaryamZi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old: https://ballerina.io/learn/by-example/backtick-templates/ new (after deploying, currently there is no such page) https://ballerina.io/learn/by-example/string-templates/