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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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.

19 changes: 12 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

"url": "string-templates",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
Expand All @@ -1111,6 +1109,13 @@
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Regex templates",
"url": "regex-templates",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion examples/raw-templates/raw_templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ 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/)
- [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/)
17 changes: 17 additions & 0 deletions examples/regex-templates/regex_templates.bal
heshanpadmasiri marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import ballerina/io;

public function main() {
string:RegExp pat1 = re `a+`;
string a0 = "";
io:println(pat1.isFullMatch(a0));
string a1 = "a";
io:println(pat1.isFullMatch(a1));
string a2 = "aa";
io:println(pat1.isFullMatch(a2));

string:RegExp pat2 = re `[a-z]+`;
string b = "bb";
io:println(pat2.isFullMatch(b));
string b1 = "bb1";
io:println(pat2.isFullMatch(b1));
}
5 changes: 5 additions & 0 deletions examples/regex-templates/regex_templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Regex templates

You can use template expressions with `re` tag to create regular expressions. Ballerina regular expressions are based on the ECMAScript 2022 in unicode mode.

+ [Regular Expressions](https://ballerina.io/learn/advanced-general-purpose-language-features/#regular-expressions)
2 changes: 2 additions & 0 deletions examples/regex-templates/regex_templates.metatags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This BBE demonstrates regex templates in Ballerina.
keywords: ballerina, ballerina by example, bbe, regex templates, regex
6 changes: 6 additions & 0 deletions examples/regex-templates/regex_templates.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$ bal run regex_template.bal
false
true
true
true
false
21 changes: 21 additions & 0 deletions examples/string-templates/string_templates.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ballerina/io;

public function main() {
// Note first \n is treated as is.
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);

// You can use interpolations to add ` and $ characters.
Copy link
Member

Choose a reason for hiding this comment

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

Need to emphasize the characters using backticks. Need to check if/how this can be done.

Copy link
Member Author

@heshanpadmasiri heshanpadmasiri Nov 10, 2024

Choose a reason for hiding this comment

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

As far as I know we can't use markdown-ish formatting in side source code, they are rendered as is (example). I didn't add backticks around ` since it would be confusing to the user (In markdown I believe you have wrap ` in side double backticks ``)

heshanpadmasiri marked this conversation as resolved.
Show resolved Hide resolved
string s2 = string `Backtick: ${"`"} Dollar: ${"$"}`;
io:println(s2);

// You can nest template expressions.
heshanpadmasiri marked this conversation as resolved.
Show resolved Hide resolved
string s3 = string `outer ${string `inner ${5} inner rest`} rest`;
io:println(s3);

// You can use an empty string interpolation to break a template expression
// across multiple lines
Copy link
Member

Choose a reason for hiding this comment

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

or break at interpolations.

Let's add examples with both.

Also need a fullstop at the end.

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't fully understand this. Do you mean

    string s4 = string `prefix ${
                ""}middle ${""  // <-
                }suffix`;

Copy link
Member

Choose a reason for hiding this comment

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

I meant breaking at a normal interpolation like

string user = "John";
string s4 = string `Hello World, from ${
                user}!`

string s4 = string `prefix ${
""}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 a `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` method and concatenated with the other string literals to form a single string literal.
heshanpadmasiri marked this conversation as resolved.
Show resolved Hide resolved
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
6 changes: 6 additions & 0 deletions examples/string-templates/string_templates.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$ bal run string_templates.bal
line one \n rest of line 1
second line
Backtick: ` Dollar: $
outer inner 5 inner rest rest
prefix suffix
Loading