Template strings (or Template literals ) in Typescript are multi-line string literals allowing string interpolation. String interpolation allows us to include embedded expressions as part of the string. You can also use multi-line strings, basic string formatting & tagged templates with Template strings. They are part of ES2016/ES6 specification. Template strings are also called as template literals. In this tutorial, we will learn the Template strings (or Literals) syntax and how to use it in Multiline Strings & string interpolation. We will also learn how to nest expressions and how to Escaping template literals using the backslash (\
)
Table of Content
Template Strings Syntax
The Syntax of template literals as shown below
1 2 3 | `string text ${expression} string text` |
- The expression must be specified inside dollar sign and curly braces
(${expression})
- We must enclose them in template literal syntax or back-tick.
- You must enable the
"target": "es6"
, or"target": "es2015",
in thetsconfig.json
file - Template literals always produce strings
Multiline strings
The following is an example of creating a multiline string. Just hit enter and continue in the next line. The \n
character is automatically gets inserted in the string.
Example
1 2 3 4 5 6 7 8 9 10 11 | let sentence: string =`Hello, welcome to the world of typescript, the typed super of javascript` console.log(sentence); //**** output **** //Hello, welcome to the world of typescript, //the typed super of javascript |
The multiline strings without back-quote are created by inserting a \n
newline character (LF).
1 2 3 4 5 6 7 8 9 10 11 | let sentence: string ="Hello, welcome to the world of typescript,\n"+ "the typed super of javascript" console.log(sentence); //***output**** //Hello, welcome to the world of typescript, //the typed super of javascript |
As we said earlier, the /n
(LF) character is inserted when you use the template strings. Hence the following code is true.
1 2 3 4 5 6 7 8 9 10 11 | const str = `Line1 Line2`; console.log(str); console.log(str === 'Line1\nLine2'); // true //***output*** //Line1 //Line2 //true |
String Interpolation
string interpolation (expression Interpolation) is the process of evaluating a string literal containing one or more expressions. It evaluates the expression and coerces the result into a string. The result is then is replaced in the original string.
Example
In the following example, $(palyerName)
is the expression. It is evaluated first and the resulting value “Sachin Tendulkar” is replaced at its place in the final string.
1 2 3 4 5 6 7 8 9 10 | //Works only with ES6/ES2015 and above let playerName:string = "Sachin Tendulkar"; console.log(`${playerName} is the greatest cricketer of all time`) //**** Output **** //Sachin Tendulkar is the greates cricker of all time |
Multiple Expressions
1 2 3 4 5 6 7 8 9 10 11 | const firstName = 'Students'; const topic ="Template Literals" console.log(`Hello ${firstName}! Welcome to the ${topic} tutorial`); //*** output **** //Hello Students! //Welcome to the Template Literals tutorial |
Using Expressions
The following example shows that you can use any expressions inside the template string. It uses the arithmetic expression ${a+b}
1 2 3 4 5 6 7 8 9 | let a=1; let b=2; console.log(`The addion of ${a} + ${b} is ${a+b}`); //***output **** //The addion of 1 + 2 is 3 |
The following example uses the ternary conditional operator(?
) in the template string.
1 2 3 4 5 6 7 8 | let m=11; console.log(`The m is ${(m==10) ?'ten':'not ten'}`); //*** output **** //The m is not ten |
String Interpolation Nesting Expressions
You can nest expressions
1 2 3 4 5 6 7 8 9 10 | let x=10 let y=20 let varxy=`${x+y}` //template string console.log(`The addion of ${x} + ${y} is ${varxy}`); //tempate string nested //*** output **** //The addion of 10 + 20 is 30 |
Escaping in template strings
The backslash \
is used for escaping inside template literals. It enables you to mention backticks and ${
inside template literals:
1 2 3 4 5 6 7 8 9 10 11 12 | console.log(```) //output ` console.log(`$`) //output $ this is ok console.log(`${`) //Error console.log(`\${`) //output ${ console.log(`\$\{\}`) //output ${} |
Tagged Templates
Another use of the Template string is in Tagged Templates. We can tag a function to a template string. This gives an opportunity to parse the template literals and control how it gets converts into a string.
You can read more about it from Tagged Templates in the next tutorial.
Reference
Summary
In this tutorial, we learned about Template Strings. They allow us to put embedded expressions inside a string using the syntax ${expression}
. We can use any expressions that return in a string. We can also use nested expressions also.
Request regarding angular typescript.
I have created a dynamic template with is supposed to be updated at run-time as its button text and some labels. but my model gets updated , view is not being changed on the map marker where the dynamic template shown
I cannot suggest anything without looking at code.